This repository has been archived by the owner on Sep 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
rancher-cli.py
executable file
·131 lines (109 loc) · 6.14 KB
/
rancher-cli.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
#!/usr/bin/python
import argparse
import json
import os
from rancher import servicelink, service, stack, config, host
def main():
parser = argparse.ArgumentParser(
description='Rancher command line client to add/remove load balancer rules.')
# Environment params
parser.add_argument('--apiUrl', default=os.environ.get('RANCHER_API_URL'),
help='api base url, $RANCHER_API_URL environment variable can be used')
parser.add_argument('--apiKey', default=os.environ.get('RANCHER_API_KEY'),
help='api key, $RANCHER_API_KEY environment variable can be used')
parser.add_argument('--apiSecret', default=os.environ.get('RANCHER_API_SECRET'),
help='api secret, $RANCHER_API_SECRET environment variable can be used')
parser.add_argument('--projectId', default=os.environ.get('RANCHER_PROJECT_ID'),
help='project environment, $RANCHER_PROJECT_ID environment variable can be used')
parser.add_argument('--loadBalancerId', default=os.environ.get('RANCHER_LB_ID'),
help='load balancer service id, $RANCHER_LB_ID environment variable can be used')
parser.add_argument('--stackName',
help='Stack name')
parser.add_argument('--stackTags',
help='Comma separated stack tags (optional)', default=None)
parser.add_argument('--stackUpgradeTimeout', default=os.environ.get('STACK_UPGRADE_TIMEOUT', 360),
help='timeout for stack upgrade in seconds. Default 360')
parser.add_argument('--stackActiveTimeout', default=os.environ.get('STACK_ACTIVE_TIMEOUT', 360),
help='timeout for stack become active in seconds. Default 360')
parser.add_argument('--stackHealthyTimeout', default=os.environ.get('STACK_HEALTHY_TIMEOUT', 360),
help='timeout for stack become healthy in seconds. Default 360')
parser.add_argument('--dockerCompose',
help='docker compose path')
parser.add_argument('--rancherCompose',
help='Rancher compose path (optional)', default=None)
parser.add_argument('--stackEnvironment', default='{}',
help='Stack environment variables json')
parser.add_argument('--stackSvc', default=None,
help='Stack/Service string')
# Action params
required_named = parser.add_argument_group('required arguments')
required_named.add_argument('--action',
help='add-link, remove-lnk, create-stack, remove-stack, get-port, get-service-port, upgrade-service, get-container-id, get-host-ip')
parser.add_argument('--serviceId',
help="""target service id. Optional, parsed from hostname if not
set by pattern: serviceName.stackName.somedomain.TLD""")
parser.add_argument('--host', help='target hostname')
parser.add_argument(
'--hostId', help='Host id where to find available port')
parser.add_argument('--portRangeStart', help='Start of desired port range')
parser.add_argument('--portRangeEnd', help='End of desired port range')
parser.add_argument(
'--externalPort', help='external service port', type=int)
parser.add_argument('--internalPort', default=None, type=int,
help='internal service port. Optional, not needed for remove action')
parser.add_argument('--data', default=None, type=str,
help='Data payload. Optional')
args = parser.parse_args()
var_args = vars(args)
internal_port = var_args.pop('internalPort')
service_id = var_args.pop('serviceId')
if args.action is None:
parser.parse_args(['-h'])
exit(2)
config.RANCHER_BASE_URL = args.apiUrl
config.RANCHER_API_ACCESS_KEY = args.apiKey
config.RANCHER_API_SECRET_KEY = args.apiSecret
config.RANCHER_PROJECT_ID = args.projectId
config.LOAD_BALANCER_SVC_ID = args.loadBalancerId
config.STACK_UPGRADE_TIMEOUT = args.stackUpgradeTimeout
config.STACK_ACTIVE_TIMEOUT = args.stackActiveTimeout
config.STACK_HEALTHY_TIMEOUT = args.stackHealthyTimeout
if service_id is None and args.host is not None:
service_id = service.parse_service_id(args.host, True)
if args.action.lower() not in ['add-link', 'remove-link', 'create-stack',
'remove-stack', 'get-port',
'get-service-port', 'update-lb',
'get-svc-id', 'get-container-id', 'get-host-ip']:
parser.parse_args(['-h'])
exit(2)
# actions
if args.action.lower() == 'get-port':
print servicelink.get_available_port(args.loadBalancerId,
int(args.portRangeStart),
int(args.portRangeEnd), service_id)
elif args.action.lower() == 'get-service-port':
print servicelink.get_service_port(service_id)
elif args.action.lower() == 'add-link':
servicelink.add_load_balancer_target(service_id, args.host, args.externalPort,
internal_port)
elif args.action.lower() == 'remove-link':
servicelink.remove_load_balancer_target(
service_id, args.host, args.externalPort)
elif args.action.lower() == 'create-stack':
stack.create(args.stackName, args.dockerCompose,
args.rancherCompose, args.stackTags)
elif args.action.lower() == 'remove-stack':
stack.remove('name', args.stackName)
elif args.action.lower() == 'update-lb':
service.update_load_balancer_service(
args.loadBalancerId, json.loads(args.data))
elif args.action.lower() == 'get-svc-id':
print service.parse_service_id(args.host)
elif args.action.lower() == 'get-container-id':
instances = service.get_service_instances(service_id)
print instances[0]['externalId']
elif args.action.lower() == 'get-host-ip':
instances = service.get_service_instances(service_id)
host_id = instances[0]['hostId']
print host.get_host_ip(host_id)
main()