-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcloud.py
executable file
·77 lines (58 loc) · 2.09 KB
/
cloud.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
#!/usr/bin/env python3
import click
from ElasticCloudAdapter import ElasticCloudAdapter
from GCEAdapter import GCEAdapter
def adapter_choice(driver):
if driver == 'gce':
return GCEAdapter()
else:
raise NotImplementedError
@click.group()
def cli():
pass
@cli.command()
@click.argument('driver', type=click.Choice(['gce']))
def auto_scale(driver):
adapter = adapter_choice(driver)
adapter.update_all_states()
states = adapter.dump_state()
output_format = '{0: <30} {1}'
click.echo(output_format.format('Name', 'State'))
for name in states:
click.echo(output_format.format(name, states[name]['status']))
next_action, action_count = adapter.get_next_action()
if next_action == ElasticCloudAdapter.ACTION_DO_NOTHING:
click.echo('Service is in equilibrium. No need to shrink or expand right now!')
if next_action == ElasticCloudAdapter.ACTION_SHRINK:
click.echo('Shrinking...')
click.echo(adapter.shrink(action_count))
if next_action == ElasticCloudAdapter.ACTION_EXPAND:
click.echo('Expanding...')
click.echo(adapter.expand(action_count))
@cli.command()
@click.argument('driver', type=click.Choice(['gce']))
def dump_state(driver):
adapter = adapter_choice(driver)
adapter.update_all_states()
states = adapter.dump_state()
print("cloud.py: after dump-state")
output_format = '{0: <30} {1}'
click.echo(output_format.format('Name', 'State'))
for name in states:
click.echo(output_format.format(name, states[name]['status']))
@cli.command()
@click.option('--n', default=1)
@click.argument('driver', type=click.Choice(['gce']))
def shrink(n, driver):
adapter = adapter_choice(driver)
click.echo('Shrinking {} nodes...'.format(n))
click.echo(adapter.shrink(n))
@cli.command()
@click.option('--n', default=1)
@click.argument('driver', type=click.Choice(['gce']))
def expand(n, driver):
adapter = adapter_choice(driver)
click.echo('Expanding {} nodes...'.format(n))
click.echo(adapter.expand(n))
if __name__ == '__main__':
cli()