11#!/usr/bin/env python
22# -*- coding: utf-8 -*-
33import os
4+ import shutil
45import sys
56import paramiko
67import traceback
@@ -12,7 +13,7 @@ import requests
1213from constants import *
1314from interactive import interactive_shell
1415from pyhero import Client
15- from prompter import PROMPTER_KWARGS
16+ from prompter import *
1617from utils import (write_to_file , set_keys_to_empty_values ,
1718 get_docker_ip_for_environment )
1819
@@ -24,7 +25,7 @@ def persist_token(user_token):
2425 Set token environment variable.
2526 """
2627 os .environ ['CLOUD_HERO_TOKEN' ] = user_token
27- write (user_token )
28+ write_token (user_token )
2829
2930
3031def forget_token ():
@@ -33,10 +34,9 @@ def forget_token():
3334 """
3435 env_token = os .environ .get (CLOUD_HERO_TOKEN_ENV_VARIABLE , None )
3536 del env_token
36- delete_token ()
3737
3838
39- def write (content ):
39+ def write_token (content ):
4040 """
4141 Write token to config file.
4242 """
@@ -57,17 +57,6 @@ def read_token():
5757 return token_from_file .strip ().replace ('\n ' , '' ).replace ('\t ' , '' )
5858
5959
60- def delete_token ():
61- """
62- Remove token.
63- """
64- config_file_path = os .path .expanduser (CLOUD_HERO_TOKEN )
65- try :
66- os .remove (config_file_path )
67- except OSError :
68- print ('You are already logged out!' )
69-
70-
7160def exception_handler (api_exception ):
7261 if isinstance (api_exception , requests .HTTPError ):
7362 response = api_exception .response
@@ -96,7 +85,8 @@ if not token:
9685 token = read_token ()
9786cloud_hero = Client (base_url = CLOUD_HERO_URI , token = token ,
9887 exception_callback = exception_handler ,
99- clean_up_arguments = True )
88+ clean_up_arguments = True ,
89+ user_agent = CLOUD_HERO_USER_AGENT )
10090
10191
10292def verify_one_of (kwargs , * args ):
@@ -112,7 +102,7 @@ def verify_one_of(kwargs, *args):
112102
113103def tags_for_pprint (tags ):
114104 if not tags :
115- return '-'
105+ return NOTHING_TO_SHOW
116106 return ', ' .join (['{}:{}' .format (key , value )
117107 for key , value in tags .items ()])
118108
@@ -146,7 +136,7 @@ def log_response(response):
146136# ------------------------------------------------------------------------
147137class OrderedHeroCommands (click .Group ):
148138
149- COMMANDS_ORDER = ['login' , 'register' , 'provider' ,
139+ COMMANDS_ORDER = ['login' , 'register' , 'provider' , 'integration' ,
150140 'environment' , 'node' , 'docker' , 'ssh' ]
151141
152142 COMMANDS_ALIASES = {
@@ -238,9 +228,9 @@ def scale_node(**kwargs):
238228@node .command ('ls' , short_help = 'List all your nodes' ,
239229 help = 'List all your nodes' )
240230def list_nodes ():
241- node_format = ('{node[name]:<25 }{environment[name]:<20}'
242- '{node[public_ip]:<17 }{node[private_ip]:<17 }'
243- '{node[status]:<10}{node[provider]:<10 }{node[id]:<25}'
231+ node_format = ('{node[name]:<20 }{environment[name]:<20}'
232+ '{node[public_ip]:<15 }{node[private_ip]:<15 }'
233+ '{node[status]:<10}{node[provider]:<15 }{node[id]:<25}'
244234 '{environment[id]:<25}{node[tags]:<15}{node[packages]:<15}' )
245235 print node_format .format (** PROMPTER_KWARGS )
246236
@@ -251,17 +241,17 @@ def list_nodes():
251241 'node' : {
252242 'id' : node ['id' ],
253243 'name' : node ['name' ],
254- 'status' : 'TBD' ,
244+ 'status' : node . get ( 'status' , NOT_AVAILABLE ) ,
255245 'provider' : environment ['provider' ]['name' ],
256- 'public_ip' : node .get ('public_ip' , '-' ),
257- 'private_ip' : node .get ('private_ip' , '-' ),
246+ 'public_ip' : node .get ('public_ip' , NOT_AVAILABLE ),
247+ 'private_ip' : node .get ('private_ip' , NOT_AVAILABLE ),
258248 'packages' : ',' .join (node ['packages' ]),
259249 'tags' : tags_for_pprint (node ['tags' ])
260250 },
261251 'environment' : {
262252 'id' : environment ['id' ],
263253 'name' : environment ['name' ],
264- }
254+ },
265255 }
266256 print (node_format .format (** node_data ))
267257
@@ -284,19 +274,18 @@ def environment():
284274 pass
285275
286276
287- @environment .command ('create' , short_help = 'Create environment' ,
288- help = 'Create environment' )
277+ @environment .command ('add' , short_help = 'Add environment' , help = 'Add environment' )
289278@click .option ('-p' , '--provider_id' , prompt = True , help = 'Select the provider ID' )
290279@click .option ('-l' , '--location' , prompt = True ,
291280 help = 'Cloud provider location/region for environment' )
292281@click .option ('-n' , '--name' , prompt = True , help = 'Environment name' )
293- def create_environment (** kwargs ):
282+ def add_environment (** kwargs ):
294283 data = {
295284 'region' : kwargs ['location' ],
296285 'environment' : kwargs ['name' ],
297286 'provider_id' : kwargs ['provider_id' ]
298287 }
299- log_response (cloud_hero .create_environment (data ))
288+ log_response (cloud_hero .add_environment (data ))
300289
301290
302291@environment .command ('rm' , short_help = 'Remove an existing environment' ,
@@ -315,7 +304,8 @@ def remove_environment(**kwargs):
315304def list_environments ():
316305 format_string = ('{node[name]:<25}{environment[location]:<15}'
317306 '{nodes_count:<15}{environment[name]:<20}'
318- '{environment[id]:<30}' )
307+ '{environment[id]:<30}'
308+ '{provider[name]:<30}{provider[id]:<30}' )
319309 print format_string .format (** PROMPTER_KWARGS )
320310
321311 environments_list = cloud_hero .list_environments ()
@@ -326,9 +316,14 @@ def list_environments():
326316 'name' : environment ['name' ],
327317 'location' : environment ['os_region' ],
328318 },
329- 'nodes_count' : len (environment ['nodes' ])
319+ 'nodes_count' : len (environment ['nodes' ]),
320+ 'provider' : {
321+ 'id' : environment ['provider' ]['id' ],
322+ 'name' : environment ['provider' ]['name' ]
323+ }
330324 }
331- for index , node in enumerate (environment ['nodes' ] or [{'name' : '-' }]):
325+ nodes_list = environment ['nodes' ] or [{'name' : NOTHING_TO_SHOW }]
326+ for index , node in enumerate (nodes_list ):
332327 if index == 0 :
333328 print format_string .format (node = node , ** environment_data )
334329 continue
@@ -446,7 +441,14 @@ def remove_provider(**kwargs):
446441def export_docker_environment (** kwargs ):
447442 environment = kwargs ['environment' ]
448443 node_details = cloud_hero .get_all_details ()
449- docker_ip = get_docker_ip_for_environment (node_details , environment )
444+ try :
445+ docker_ip = get_docker_ip_for_environment (node_details , environment )
446+ except NotFound as exception :
447+ sys .exit (exception .message )
448+ if docker_ip is None :
449+ sys .exit ('Node is still being created, could not find the IP to '
450+ 'connect to' )
451+
450452 print ('export DOCKER_HOST=tcp://{}:4000' .format (docker_ip ))
451453 print ('# Run this command to configure your shell:\n '
452454 '# eval "$(hero docker {})"' .format (environment ))
@@ -520,7 +522,7 @@ def ssh_to_vm(**kwargs):
520522 remote_user = node ['provider' ]['username' ]
521523
522524 # Get key and write it to the local path
523- expanded_file_path = os .path .expanduser (SSH_KEY_PATH )
525+ expanded_file_path = os .path .expanduser (CLOUD_HERO_SSH_KEY )
524526 if not os .path .exists (expanded_file_path ):
525527 ssh_key_content = cloud_hero .list_key ()['content' ]
526528 write_to_file (ssh_key_content , expanded_file_path )
@@ -558,6 +560,15 @@ def ssh_to_vm(**kwargs):
558560def logout ():
559561 forget_token ()
560562
563+ # Remove everything from the CLOUD_HERO_PATH.
564+ cloud_hero_path = os .path .expanduser (CLOUD_HERO_DIR )
565+ try :
566+ shutil .rmtree (cloud_hero_path )
567+ except OSError as os_exception :
568+ print (os_exception .strerror )
569+
570+ print ('Logout successful!' )
571+
561572
562573@hero .command (short_help = 'Login user' , help = 'Login user' )
563574@click .option ('-e' , '--email' , prompt = True , help = 'Email' )
0 commit comments