update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b second try
[osm/SO.git] / rwlaunchpad / plugins / rwimagemgr / bin / upload_image.py
1 #!/usr/bin/env python3
2
3 #
4 # Copyright 2016 RIFT.IO Inc
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18
19
20 import argparse
21 import asyncio
22 import logging
23 import sys
24
25 from rift.tasklets.rwimagemgr import tasklet, glance_client
26 from rift.mano.cloud import accounts
27
28 import gi
29 gi.require_version('RwCloudYang', '1.0')
30 gi.require_version('RwLog', '1.0')
31 from gi.repository import (
32 RwCloudYang,
33 RwLog,
34 )
35
36 openstack_info = {
37 'username': 'pluto',
38 'password': 'mypasswd',
39 'project_name': 'demo',
40 'auth_url': 'http://10.66.4.18:5000/v3',
41 'mgmt_network': 'private'
42 }
43
44
45 def create_account(log):
46 account_msg = RwCloudYang.YangData_RwProject_Project_CloudAccounts_CloudAccountList.from_dict(dict(
47 name="openstack",
48 account_type="openstack",
49 openstack=dict(
50 key=openstack_info["username"],
51 secret=openstack_info["password"],
52 tenant=openstack_info["project_name"],
53 auth_url=openstack_info["auth_url"]
54 )
55 )
56 )
57
58 account = accounts.CloudAccount(
59 log,
60 RwLog.Ctx.new(__file__),
61 account_msg
62 )
63
64 return account
65
66
67 def parse_args(argv=sys.argv[1:]):
68 parser = argparse.ArgumentParser()
69 parser.add_argument("--image-name", required=True)
70 parser.add_argument("--image-checksum", required=True)
71
72 return parser.parse_args()
73
74
75 def main():
76 args = parse_args()
77 logging.basicConfig(level=logging.DEBUG)
78 log = logging.getLogger("upload_image.py")
79 loop = asyncio.get_event_loop()
80 cloud_account = create_account(log)
81 client = glance_client.OpenstackGlanceClient.from_token(
82 log, "127.0.0.1", 9292, "test"
83 )
84 task_creator = tasklet.GlanceClientUploadTaskCreator(
85 log, loop, {"openstack": cloud_account}, client,
86 )
87
88 tasks = loop.run_until_complete(
89 task_creator.create_tasks(
90 ["openstack"],
91 args.image_name,
92 args.image_checksum
93 )
94 )
95
96 log.debug("Created tasks: %s", tasks)
97
98 log.debug("uploading images")
99 loop.run_until_complete(asyncio.wait([t.start() for t in tasks], loop=loop))
100
101
102 if __name__ == "__main__":
103 main()