Browse code

Merge "Update clouds.yaml"

Jenkins authored on 2015/06/25 20:58:31
Showing 2 changed files
... ...
@@ -1295,28 +1295,31 @@ fi
1295 1295
 # Save some values we generated for later use
1296 1296
 save_stackenv
1297 1297
 
1298
-# Write out a clouds.yaml file
1299
-# putting the location into a variable to allow for easier refactoring later
1300
-# to make it overridable. There is current no usecase where doing so makes
1301
-# sense, so I'm not actually doing it now.
1298
+# Update/create user clouds.yaml file.
1299
+# clouds.yaml will have a `devstack` entry for the `demo` user for the `demo`
1300
+# project.
1301
+
1302
+# The location is a variable to allow for easier refactoring later to make it
1303
+# overridable. There is currently no usecase where doing so makes sense, so
1304
+# it's not currently configurable.
1302 1305
 CLOUDS_YAML=~/.config/openstack/clouds.yaml
1303
-if [ ! -e $CLOUDS_YAML ]; then
1304
-    mkdir -p $(dirname $CLOUDS_YAML)
1305
-    cat >"$CLOUDS_YAML" <<EOF
1306
-clouds:
1307
-  devstack:
1308
-    auth:
1309
-      auth_url: $KEYSTONE_AUTH_URI/v$IDENTITY_API_VERSION
1310
-      username: demo
1311
-      project_name: demo
1312
-      password: $ADMIN_PASSWORD
1313
-    region_name: $REGION_NAME
1314
-    identity_api_version: $IDENTITY_API_VERSION
1315
-EOF
1316
-    if [ -f "$SSL_BUNDLE_FILE" ]; then
1317
-        echo "    cacert: $SSL_BUNDLE_FILE" >>"$CLOUDS_YAML"
1318
-    fi
1319
-fi
1306
+
1307
+mkdir -p $(dirname $CLOUDS_YAML)
1308
+
1309
+CA_CERT_ARG=''
1310
+if [ -f "$SSL_BUNDLE_FILE" ]; then
1311
+    CA_CERT_ARG="--os-cacert $SSL_BUNDLE_FILE"
1312
+fi
1313
+$TOP_DIR/tools/update_clouds_yaml.py \
1314
+    --file $CLOUDS_YAML \
1315
+    --os-cloud devstack \
1316
+    --os-region-name $REGION_NAME \
1317
+    --os-identity-api-version $IDENTITY_API_VERSION \
1318
+    $CA_CERT_ARG \
1319
+    --os-auth-url $KEYSTONE_AUTH_URI/v$IDENTITY_API_VERSION \
1320
+    --os-username demo \
1321
+    --os-password $ADMIN_PASSWORD \
1322
+    --os-project-name demo
1320 1323
 
1321 1324
 
1322 1325
 # Wrapup configuration
1323 1326
new file mode 100755
... ...
@@ -0,0 +1,95 @@
0
+#!/usr/bin/env python
1
+
2
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
3
+# not use this file except in compliance with the License. You may obtain
4
+# a copy of the License at
5
+#
6
+#      http://www.apache.org/licenses/LICENSE-2.0
7
+#
8
+# Unless required by applicable law or agreed to in writing, software
9
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11
+# License for the specific language governing permissions and limitations
12
+# under the License.
13
+
14
+# Update the clouds.yaml file.
15
+
16
+
17
+import argparse
18
+import os.path
19
+
20
+import yaml
21
+
22
+
23
+class UpdateCloudsYaml(object):
24
+    def __init__(self, args):
25
+        if args.file:
26
+            self._clouds_path = args.file
27
+            self._create_directory = False
28
+        else:
29
+            self._clouds_path = os.path.expanduser(
30
+                '~/.config/openstack/clouds.yaml')
31
+            self._create_directory = True
32
+        self._clouds = {}
33
+
34
+        self._cloud = args.os_cloud
35
+        self._cloud_data = {
36
+            'region_name': args.os_region_name,
37
+            'identity_api_version': args.os_identity_api_version,
38
+            'auth': {
39
+                'auth_url': args.os_auth_url,
40
+                'username': args.os_username,
41
+                'password': args.os_password,
42
+                'project_name': args.os_project_name,
43
+            },
44
+        }
45
+        if args.os_cacert:
46
+            self._cloud_data['cacert'] = args.os_cacert
47
+
48
+    def run(self):
49
+        self._read_clouds()
50
+        self._update_clouds()
51
+        self._write_clouds()
52
+
53
+    def _read_clouds(self):
54
+        try:
55
+            with open(self._clouds_path) as clouds_file:
56
+                self._clouds = yaml.load(clouds_file)
57
+        except IOError:
58
+            # The user doesn't have a clouds.yaml file.
59
+            print("The user clouds.yaml file didn't exist.")
60
+            self._clouds = {}
61
+
62
+    def _update_clouds(self):
63
+        self._clouds.setdefault('clouds', {})[self._cloud] = self._cloud_data
64
+
65
+    def _write_clouds(self):
66
+
67
+        if self._create_directory:
68
+            clouds_dir = os.path.dirname(self._clouds_path)
69
+            os.makedirs(clouds_dir)
70
+
71
+        with open(self._clouds_path, 'w') as clouds_file:
72
+            yaml.dump(self._clouds, clouds_file, default_flow_style=False)
73
+
74
+
75
+def main():
76
+    parser = argparse.ArgumentParser('Update clouds.yaml file.')
77
+    parser.add_argument('--file')
78
+    parser.add_argument('--os-cloud', required=True)
79
+    parser.add_argument('--os-region-name', default='RegionOne')
80
+    parser.add_argument('--os-identity-api-version', default='3')
81
+    parser.add_argument('--os-cacert')
82
+    parser.add_argument('--os-auth-url', required=True)
83
+    parser.add_argument('--os-username', required=True)
84
+    parser.add_argument('--os-password', required=True)
85
+    parser.add_argument('--os-project-name', required=True)
86
+
87
+    args = parser.parse_args()
88
+
89
+    update_clouds_yaml = UpdateCloudsYaml(args)
90
+    update_clouds_yaml.run()
91
+
92
+
93
+if __name__ == "__main__":
94
+    main()