Browse Source

Support different output formats + metadata cleaning

Sebastian Lohff 7 months ago
parent
commit
d67aadd038
1 changed files with 31 additions and 9 deletions
  1. 31
    9
      genconfdrv

+ 31
- 9
genconfdrv View File

@@ -25,6 +25,7 @@ class ConfigDrive:
25 25
         self._interfaces = []
26 26
         self._pubkeys = []
27 27
         self._verbose = verbose
28
+        self._clean_metadata = False
28 29
 
29 30
         self._added_resolv_module_call = False
30 31
 
@@ -92,6 +93,9 @@ class ConfigDrive:
92 93
             self.add_command("cloud-init single --name cc_resolv_conf", True)
93 94
             self._added_resolv_module_call = True
94 95
 
96
+    def set_clean_metadata(self, do_clean_metadata):
97
+        self._clean_metadata = do_clean_metadata
98
+
95 99
     def add_user(self, name, keys=None, gecos=None, sudo=False, password=None):
96 100
         if "users" not in self._user_data:
97 101
             self._user_data["users"] = []
@@ -168,8 +172,10 @@ class ConfigDrive:
168 172
             meta_data["files"].append({"content_path": "/content/0001", "path": "/etc/cloud/cloud.cfg.d/99-disable-network-config.cfg"})
169 173
             self.add_text("/openstack/content/0001", "network: {config: disabled}")
170 174
 
171
-        #meta_data["files"].append({"content_path": "/content/0002", "path": "/etc/cloud/cloud.cfg.d/10-resolv-conf.cfg"})
172
-        #self.add_text("/openstack/content/0002", "cloud_init_modules:\n  - resolv-conf\n")
175
+        # do not look for datasource on every boot
176
+        if self._clean_metadata:
177
+            meta_data["files"].append({"content_path": "/content/0002", "path": "/etc/cloud/cloud.cfg.d/99-manual-cache-clean.cfg"})
178
+            self.add_text("/openstack/content/0002", "manual_cache_clean: True")
173 179
 
174 180
         if self._pubkeys:
175 181
             meta_data["public_keys"] = {}
@@ -204,10 +210,18 @@ class ConfigDrive:
204 210
     def _write_userdata(self):
205 211
         self.add_text("/openstack/latest/user_data", "#cloud-config\n" + json.dumps(self._user_data, indent=4))
206 212
 
207
-    def write_iso(self, path):
213
+    def write_drive(self, path, fmt):
208 214
         self._write_metadata()
209 215
         self._write_userdata()
210 216
 
217
+        if fmt == "iso":
218
+            self._write_iso(path)
219
+        elif fmt == "tgz":
220
+            self._write_tgz(path)
221
+        else:
222
+            raise ValueError("Unknown format")
223
+
224
+    def _write_iso(self, path):
211 225
         p = subprocess.Popen([self._genisoimage,
212 226
                               "-J", "-r", "-q",
213 227
                               "-V", "config-2",
@@ -220,6 +234,10 @@ class ConfigDrive:
220 234
 
221 235
         return p.wait()
222 236
 
237
+    def _write_tgz(self, path):
238
+        p = subprocess.Popen(["tar", "cfz", path, "-C", self._tmpfs.getsyspath(""), "."])
239
+        return p.wait()
240
+
223 241
     def close(self):
224 242
         if self._tmpfs:
225 243
             self._tmpfs.close()
@@ -254,6 +272,14 @@ def main():
254 272
 
255 273
     args = parser.parse_args()
256 274
 
275
+    if not args.format:
276
+        if args.output.endswith(".tar.gz") or args.output.endswith(".tgz"):
277
+            args.format = "tgz"
278
+        elif args.output.endswith(".iso"):
279
+            args.format = "iso"
280
+        else:
281
+            parser.error("Could not infer output format from output file extension")
282
+
257 283
     cfgdrv = None
258 284
     try:
259 285
         cfgdrv = ConfigDrive(verbose=args.verbose)
@@ -261,10 +287,7 @@ def main():
261 287
         cfgdrv.set_hostname(args.hostname)
262 288
 
263 289
         for net in args.networks:
264
-            if ";" in net:
265
-                net = net.split(";")
266
-            else:
267
-                net = net.split(":")
290
+            net = net.split(":")
268 291
             cfgdrv.conf_network(*net)
269 292
 
270 293
         if args.nameservers:
@@ -311,8 +334,7 @@ def main():
311 334
                 cfgdrv.add_user(user[0], keys, sudo=sudo, gecos=gecos, password=password)
312 335
 
313 336
         if args.output:
314
-            ret = cfgdrv.write_iso(args.output)
315
-            sys.exit(ret)
337
+            cfgdrv.write_iso(args.output)
316 338
     finally:
317 339
         if cfgdrv:
318 340
             cfgdrv.close()

Loading…
Cancel
Save