-
Notifications
You must be signed in to change notification settings - Fork 7
/
generate-appliance-json.py
executable file
·132 lines (110 loc) · 3.53 KB
/
generate-appliance-json.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python
"""Uploads a CoreOS image to the OpenNebula MarketPlace."""
import argparse
import hashlib
import json
import os
import sys
SHORT_DESCRIPTION_TEMPLATE = """
CoreOS %(channel)s image for KVM hosts under OpenNebula.
""".strip()
DESCRIPTION_TEMPLATE = """
Password for user `core` is disabled. You will need an SSH key in order to
log in.
This image works best with two network interfaces defined:
* The first network interface will be used as CoreOS' private IPv4
address.
* If there is a second network interface defined, it will be used as
CoreOS' public IPv4 network.
The VM template bundled with this image includes a `USER_DATA` field, with
which you may pass extra
[cloud-config](https://coreos.com/os/docs/latest/cloud-config.html)
user data to configure your CoreOS instance.
The source code for this image is available at:
<https://github.com/carletes/coreos-opennebula-image>
Contributions are most welcome!
""".strip()
IMAGE_TEMPLATE_TEXT = json.dumps({
"CONTEXT": {
"NETWORK": "YES",
"SET_HOSTNAME": "$NAME",
"SSH_PUBLIC_KEY": "$USER[SSH_PUBLIC_KEY]",
"USER_DATA": "$USER_DATA",
},
"CPU": "1",
"GRAPHICS": {
"LISTEN": "0.0.0.0",
"TYPE": "vnc"
},
"MEMORY": "512",
"OS": {
"ARCH": "x86_64"
},
"USER_INPUTS": {
"USER_DATA": "M|text|User data for `cloud-config`",
}
})
def main():
p = argparse.ArgumentParser(description=__doc__.strip())
p.add_argument("channel",
help="CoreOS channel of the image")
p.add_argument("version",
help="CoreOS version of the image")
p.add_argument("image",
help="path to the qcow2 image file")
p.add_argument("url",
help="URL of qcow2 image file")
p.add_argument("--output",
help=("path to the outpu JSON file. If not given, the "
"image will not be uploaded."))
args = p.parse_args()
vars = {
"channel": args.channel,
"version": args.version,
}
hypervisor = "KVM"
image_fmt = "qcow2"
os_arch = "x86_64"
os_id = "CoreOS"
os_release = "%s (%s channel)" % (args.version, args.channel)
appliance = json.dumps({
"name": "CoreOS %s" % (args.channel,),
"short_description": SHORT_DESCRIPTION_TEMPLATE % vars,
"description": DESCRIPTION_TEMPLATE % vars,
"version": args.version,
"opennebula_version": "4.14",
"files": [
{
"name": "coreos-%s-%s" % (args.channel, args.version),
"url": args.url,
"size": str(os.stat(args.image).st_size),
"md5": md5_hash(args.image),
"compression": "bz2",
"driver": image_fmt,
"type": "OS",
"hypervisor": hypervisor,
"format": image_fmt,
"os-id": os_id,
"os-release": os_release,
"os-arch": os_arch,
}
],
"hypervisor": hypervisor,
"format": image_fmt,
"os-id": os_id,
"os-release": os_release,
"os-arch": os_arch,
"opennebula_template": IMAGE_TEMPLATE_TEXT,
}, indent=4)
if args.output is None:
print appliance
return
with open(args.output, "w") as f:
f.write(appliance)
def md5_hash(fname):
md5 = hashlib.md5()
with open(fname, "rb") as f:
md5.update(f.read())
return md5.hexdigest()
if __name__ == "__main__":
sys.exit(main())