|
| 1 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 | +# not use this file except in compliance with the License. You may obtain |
| 3 | +# a copy of the License at |
| 4 | +# |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | +# License for the specific language governing permissions and limitations |
| 11 | +# under the License. |
| 12 | + |
| 13 | +""" |
| 14 | +Example Create a jenkins server |
| 15 | +
|
| 16 | +Create all the pieces parts to get a jenkins server up and running. |
| 17 | +
|
| 18 | +To run: |
| 19 | + python examples/jenkins.py |
| 20 | +""" |
| 21 | + |
| 22 | +import base64 |
| 23 | +import os |
| 24 | +import sys |
| 25 | + |
| 26 | +from examples import common |
| 27 | +from openstack import connection |
| 28 | +from openstack import exceptions |
| 29 | + |
| 30 | + |
| 31 | +def create_jenkins(opts): |
| 32 | + name = opts.data.pop('name', 'jenkins') |
| 33 | + dns_nameservers = opts.data.pop('dns_nameservers', '206.164.176.34') |
| 34 | + cidr = opts.data.pop('cidr', '10.3.3.0/24') |
| 35 | + flavor = opts.data.pop('flavor', '103') |
| 36 | + image = opts.data.pop('image', 'bec3cab5-4722-40b9-a78a-3489218e22fe') |
| 37 | + |
| 38 | + args = vars(opts) |
| 39 | + conn = connection.Connection(preference=opts.user_preferences, **args) |
| 40 | + |
| 41 | + try: |
| 42 | + network = conn.network.find_network(name) |
| 43 | + except exceptions.ResourceNotFound: |
| 44 | + network = conn.network.create_network(name=name) |
| 45 | + print(str(network)) |
| 46 | + |
| 47 | + try: |
| 48 | + subnet = conn.network.find_subnet(name) |
| 49 | + except exceptions.ResourceNotFound: |
| 50 | + args = { |
| 51 | + "name": name, |
| 52 | + "network_id": network.id, |
| 53 | + "ip_version": "4", |
| 54 | + "dns_nameservers": [dns_nameservers], |
| 55 | + "cidr": cidr, |
| 56 | + } |
| 57 | + subnet = conn.network.create_subnet(**args) |
| 58 | + print(str(subnet)) |
| 59 | + |
| 60 | + extnet = conn.network.find_network("Ext-Net") |
| 61 | + try: |
| 62 | + router = conn.network.find_router(name) |
| 63 | + except exceptions.ResourceNotFound: |
| 64 | + args = { |
| 65 | + "name": name, |
| 66 | + "external_gateway_info": {"network_id": extnet.id} |
| 67 | + } |
| 68 | + router = conn.network.create_router(**args) |
| 69 | + conn.network.router_add_interface(router, subnet.id) |
| 70 | + print(str(router)) |
| 71 | + |
| 72 | + try: |
| 73 | + sg = conn.network.find_security_group(name) |
| 74 | + except exceptions.ResourceNotFound: |
| 75 | + sg = conn.network.create_security_group(name=name) |
| 76 | + print(str(sg)) |
| 77 | + rule = { |
| 78 | + 'direction': 'ingress', |
| 79 | + 'remote_ip_prefix': '0.0.0.0/0', |
| 80 | + 'protocol': 'tcp', |
| 81 | + 'port_range_max': 9022, |
| 82 | + 'port_range_min': 9022, |
| 83 | + 'security_group_id': sg.id, |
| 84 | + 'ethertype': 'IPv4' |
| 85 | + } |
| 86 | + conn.network.create_security_group_rule(**rule) |
| 87 | + print('rule allow 9022') |
| 88 | + rule = { |
| 89 | + 'direction': 'ingress', |
| 90 | + 'remote_ip_prefix': '0.0.0.0/0', |
| 91 | + 'protocol': 'tcp', |
| 92 | + 'port_range_max': 443, |
| 93 | + 'port_range_min': 443, |
| 94 | + 'security_group_id': sg.id, |
| 95 | + 'ethertype': 'IPv4' |
| 96 | + } |
| 97 | + conn.network.create_security_group_rule(**rule) |
| 98 | + print('rule allow HTTPS') |
| 99 | + rule = { |
| 100 | + 'direction': 'ingress', |
| 101 | + 'remote_ip_prefix': '0.0.0.0/0', |
| 102 | + 'protocol': 'icmp', |
| 103 | + 'port_range_max': None, |
| 104 | + 'port_range_min': None, |
| 105 | + 'security_group_id': sg.id, |
| 106 | + 'ethertype': 'IPv4' |
| 107 | + } |
| 108 | + conn.network.create_security_group_rule(**rule) |
| 109 | + print('rule allow ping') |
| 110 | + rule = { |
| 111 | + 'direction': 'ingress', |
| 112 | + 'remote_ip_prefix': '0.0.0.0/0', |
| 113 | + 'protocol': 'tcp', |
| 114 | + 'port_range_max': 80, |
| 115 | + 'port_range_min': 80, |
| 116 | + 'security_group_id': sg.id, |
| 117 | + 'ethertype': 'IPv4' |
| 118 | + } |
| 119 | + conn.network.create_security_group_rule(**rule) |
| 120 | + print('rule allow HTTP') |
| 121 | + rule = { |
| 122 | + 'direction': 'ingress', |
| 123 | + 'remote_ip_prefix': None, |
| 124 | + 'protocol': None, |
| 125 | + 'port_range_max': None, |
| 126 | + 'port_range_min': None, |
| 127 | + 'security_group_id': sg.id, |
| 128 | + 'ethertype': 'IPv6' |
| 129 | + } |
| 130 | + conn.network.create_security_group_rule(**rule) |
| 131 | + print('rule allow IPv6') |
| 132 | + rule = { |
| 133 | + 'direction': 'ingress', |
| 134 | + 'remote_ip_prefix': '0.0.0.0/0', |
| 135 | + 'protocol': 'tcp', |
| 136 | + 'port_range_max': 8080, |
| 137 | + 'port_range_min': 8080, |
| 138 | + 'security_group_id': sg.id, |
| 139 | + 'ethertype': 'IPv4' |
| 140 | + } |
| 141 | + conn.network.create_security_group_rule(**rule) |
| 142 | + print('rule allow 8080') |
| 143 | + rule = { |
| 144 | + 'direction': 'ingress', |
| 145 | + 'remote_ip_prefix': '0.0.0.0/0', |
| 146 | + 'protocol': 'tcp', |
| 147 | + 'port_range_max': 4222, |
| 148 | + 'port_range_min': 4222, |
| 149 | + 'security_group_id': sg.id, |
| 150 | + 'ethertype': 'IPv4' |
| 151 | + } |
| 152 | + conn.network.create_security_group_rule(**rule) |
| 153 | + print('rule allow 4222') |
| 154 | + rule = { |
| 155 | + 'direction': 'ingress', |
| 156 | + 'remote_ip_prefix': '0.0.0.0/0', |
| 157 | + 'protocol': 'tcp', |
| 158 | + 'port_range_max': 22, |
| 159 | + 'port_range_min': 22, |
| 160 | + 'security_group_id': sg.id, |
| 161 | + 'ethertype': 'IPv4' |
| 162 | + } |
| 163 | + conn.network.create_security_group_rule(**rule) |
| 164 | + print('rule allow ssh') |
| 165 | + print(str(sg)) |
| 166 | + |
| 167 | + try: |
| 168 | + kp = conn.compute.find_keypair(name) |
| 169 | + except exceptions.ResourceNotFound: |
| 170 | + kp = conn.compute.create_keypair(name=name) |
| 171 | + try: |
| 172 | + os.remove('jenkins') |
| 173 | + except OSError: |
| 174 | + pass |
| 175 | + try: |
| 176 | + os.remove('jenkins.pub') |
| 177 | + except OSError: |
| 178 | + pass |
| 179 | + print(str(kp)) |
| 180 | + f = open('jenkins', 'w') |
| 181 | + f.write("%s" % kp.private_key) |
| 182 | + f.close() |
| 183 | + f = open('jenkins.pub', 'w') |
| 184 | + f.write("%s" % kp.public_key) |
| 185 | + f.close() |
| 186 | + print(str(kp)) |
| 187 | + |
| 188 | + try: |
| 189 | + server = conn.compute.find_server(name) |
| 190 | + server = conn.get(server) |
| 191 | + except exceptions.ResourceNotFound: |
| 192 | + f = open('examples/cloud-init.sh', 'r') |
| 193 | + cmd = f.read() |
| 194 | + f.close() |
| 195 | + b64str = base64.b64encode(cmd) |
| 196 | + args = { |
| 197 | + "name": name, |
| 198 | + "flavorRef": flavor, |
| 199 | + "imageRef": image, |
| 200 | + "imageRef": image, |
| 201 | + "key_name": name, |
| 202 | + "networks": [{"uuid": network.id}], |
| 203 | + "user_data": b64str, |
| 204 | + } |
| 205 | + server = conn.compute.create_server(**args) |
| 206 | + print(str(server)) |
| 207 | + print('Waiting for the server to come up....') |
| 208 | + conn.compute.wait_for_status(server) |
| 209 | + print('Server is up.') |
| 210 | + |
| 211 | + if len(server.get_floating_ips()) <= 0: |
| 212 | + try: |
| 213 | + ip = conn.network.find_available_ip() |
| 214 | + except exceptions.ResourceNotFound: |
| 215 | + ip = conn.network.create_ip(floating_network_id=extnet.id) |
| 216 | + port = next(conn.network.list_ports(device_id=server.id, fields='id')) |
| 217 | + conn.network.add_ip_to_port(port, ip) |
| 218 | + print(str(port)) |
| 219 | + ip = conn.get(ip) |
| 220 | + print("ssh -i jenkins ubuntu@%s" % ip.floating_ip_address) |
| 221 | + print("http://%s:8080" % ip.floating_ip_address) |
| 222 | + |
| 223 | + return |
| 224 | + |
| 225 | + |
| 226 | +def delete_jenkins(opts): |
| 227 | + name = opts.data.pop('name', 'jenkins') |
| 228 | + args = vars(opts) |
| 229 | + conn = connection.Connection(preference=opts.user_preferences, **args) |
| 230 | + |
| 231 | + try: |
| 232 | + server = conn.compute.find_server(name) |
| 233 | + server = conn.get(server) |
| 234 | + print(str(server)) |
| 235 | + ips = server.get_floating_ips() |
| 236 | + for ip in ips: |
| 237 | + print(str(ip)) |
| 238 | + ip = conn.network.find_ip(ip) |
| 239 | + conn.network.remove_ip_from_port(ip) |
| 240 | + conn.delete(ip) |
| 241 | + conn.delete(server) |
| 242 | + except exceptions.ResourceNotFound: |
| 243 | + pass |
| 244 | + |
| 245 | + try: |
| 246 | + kp = conn.compute.find_keypair(name) |
| 247 | + print(str(kp)) |
| 248 | + conn.delete(kp) |
| 249 | + except exceptions.ResourceNotFound: |
| 250 | + pass |
| 251 | + |
| 252 | + try: |
| 253 | + router = conn.network.find_router(name) |
| 254 | + print(str(router)) |
| 255 | + except exceptions.ResourceNotFound: |
| 256 | + router = None |
| 257 | + pass |
| 258 | + |
| 259 | + try: |
| 260 | + subnet = conn.network.find_subnet(name) |
| 261 | + print(str(subnet)) |
| 262 | + if router: |
| 263 | + try: |
| 264 | + conn.network.router_remove_interface(router, subnet.id) |
| 265 | + except Exception: |
| 266 | + pass |
| 267 | + for port in conn.network.get_subnet_ports(subnet.id): |
| 268 | + print(str(port)) |
| 269 | + conn.delete(port) |
| 270 | + except exceptions.ResourceNotFound: |
| 271 | + subnet = None |
| 272 | + pass |
| 273 | + |
| 274 | + try: |
| 275 | + if router: |
| 276 | + conn.delete(router) |
| 277 | + except exceptions.ResourceNotFound: |
| 278 | + pass |
| 279 | + |
| 280 | + try: |
| 281 | + if subnet: |
| 282 | + conn.delete(subnet) |
| 283 | + except exceptions.ResourceNotFound: |
| 284 | + pass |
| 285 | + |
| 286 | + try: |
| 287 | + network = conn.network.find_network(name) |
| 288 | + print(str(network)) |
| 289 | + conn.delete(network) |
| 290 | + except exceptions.ResourceNotFound: |
| 291 | + pass |
| 292 | + |
| 293 | + |
| 294 | +def run_jenkins(opts): |
| 295 | + argument = opts.argument |
| 296 | + if argument == "delete": |
| 297 | + return(delete_jenkins(opts)) |
| 298 | + return(create_jenkins(opts)) |
| 299 | + |
| 300 | + |
| 301 | +if __name__ == "__main__": |
| 302 | + opts = common.setup() |
| 303 | + sys.exit(common.main(opts, run_jenkins)) |
0 commit comments