def connect(env_name): juju_home = os.path.expanduser(os.environ.get("JUJU_HOME", "~/.juju")) juju_conf = os.path.join(juju_home, "environments", "%s.jenv" % env_name) conf = _config_from_env(juju_conf) env = Environment(conf['uri']) env.login(conf['secret']) return env
class JujuCommunication: """ Manage the communication with the Juju controller """ def __init__(self, creds): """ Constructor, initializes the connection to the Juju Controller :param creds: information to get a connection to the Juju Controller :exception JujuError: raised if connection to Juju can't be done """ self.logger = logging.getLogger(__name__) self._creds = creds self.client = None def connect(self): self.logger.info("Connection with the JUJU Controller") self.logger.debug("Server (%s:%s)", self._creds['address'], self._creds['port']) self.logger.debug("Token (%s)", self._creds['token']) if not self.client: try: try: self.client = Environment("wss://{}:{}".format(self._creds['address'], self._creds['port'])) self.client.login(self._creds['token']) except socket.error as err: if not err.errno in (errno.ETIMEDOUT, errno.ECONNREFUSED, errno.ECONNRESET): raise JujuError("Cannot reach endpoint provided (%s:%s)" % (self._creds['address'], self._creds['port']), err.message) except EnvError, e: raise JujuError("Communication with Juju can't be performed", e.message) self.logger.info("Connection successfully established")
class Environment: def __init__(self, options): env_path = os.path.expanduser( "~/.juju/environments/%s.jenv" % self.name) if not os.path.exists(env_path): raise Exception("Not found specified environment: %s" % self.name) with open(env_path) as fd: loaded = yaml.load(fd.read()) for k, v in loaded.items(): setattr(self, k.replace("-", "_"), v) self._env = GoEnvironment("wss://{}".format(self.state_servers[0])) self._env.login(self.password) self.options = options try: provider_type = self.bootstrap_config.get('type') klass = "{0}Provider".format(provider_type.capitalize()) self.provider = getattr(providers, klass)(self) except AttributeError: raise Exception("Provider type: %s , not supported yet" % provider_type) except: raise @property def name(self): if hasattr(self, '_name'): return self._name name = os.environ.get("JUJU_ENV", None) if name: self._name = name else: self._name = yaml.load( subprocess.check_output(["juju", "get-environment"]))['name'] return self._name @property def status(self): return self._env.status() @property def machines(self): return self.status.get('Machines').items() def suspend(self): return self.provider.suspend() def resume(self): return self.provider.resume()
def get_env(cls, name=None, user=None, password=None): # A hook env will have this set api_addresses = os.environ.get('JUJU_API_ADDRESSES') if not api_addresses: # use the local option/connect which # parses local jenv info env = Environment.connect(name) else: env = Environment(api_addresses.split()[0]) env.login(user=user, password=password) return env
def __init__(self, options): env_path = os.path.expanduser( "~/.juju/environments/%s.jenv" % self.name) if not os.path.exists(env_path): raise Exception("Not found specified environment: %s" % self.name) with open(env_path) as fd: loaded = yaml.load(fd.read()) for k, v in loaded.items(): setattr(self, k.replace("-", "_"), v) self._env = GoEnvironment("wss://{}".format(self.state_servers[0])) self._env.login(self.password) self.options = options try: provider_type = self.bootstrap_config.get('type') klass = "{0}Provider".format(provider_type.capitalize()) self.provider = getattr(providers, klass)(self) except AttributeError: raise Exception("Provider type: %s , not supported yet" % provider_type) except: raise
def connect_state_server(self): while True: try: log.debug('Connecting to state server for environment: %s', self.config.environment) return Environment.connect(self.config.environment) except KeyboardInterrupt: return except: log.exception('Could not connect to state server') log.debug('Retrying connection in 30 seconds') time.sleep(30)
def run(self): Gdk.threads_enter() try: env = Environment.connect(self.environment) status = env.status() machines = status.get('Machines', None).values() results = [] for machine in machines: results.append(Machine(machine)) self.emit('on_status', self.environment, results) except Exception as ex: self.emit('on_status_error', ex) finally: Gdk.threads_leave()
def connect(self): self.logger.info("Connection with the JUJU Controller") self.logger.debug("Server (%s:%s)", self._creds['address'], self._creds['port']) self.logger.debug("Token (%s)", self._creds['token']) if not self.client: try: try: self.client = Environment("wss://{}:{}".format(self._creds['address'], self._creds['port'])) self.client.login(self._creds['token']) except socket.error as err: if not err.errno in (errno.ETIMEDOUT, errno.ECONNREFUSED, errno.ECONNRESET): raise JujuError("Cannot reach endpoint provided (%s:%s)" % (self._creds['address'], self._creds['port']), err.message) except EnvError, e: raise JujuError("Communication with Juju can't be performed", e.message) self.logger.info("Connection successfully established")
def run(self): Gdk.threads_enter() try: env = Environment.connect(self.environment) r = env.add_machine(series=self.series, constraints=self.constraints) machine_id = r.get('Machine') logger.debug( "Threaded watcher for id:%s on %s" % (machine_id, __name__)) threading.Thread(target=env.wait_for_machines, args=(machine_id, ), kwargs={ 'callbacks': { 'on_complete': self.on_machine_complete, 'on_process': self.on_machine_process, } }).start() except Exception as ex: self.emit('on_machine_error', ex) finally: Gdk.threads_leave()
def connect(self): # TODO TLS Conn Cache, underlying socket supports async, but client lib # we're using is synchronous based. return Client.connect(self.config.get_env_name())
def setUp(self): self.env = Environment.connect(ENV_NAME) self.bm = Backups(self.env)
def setUp(self): self.env = Environment.connect(ENV_NAME) self.keys = KeyManager(self.env)
def setUp(self): self.env = Environment.connect(ENV_NAME)
def setUp(self): self.env = Environment.connect(ENV_NAME) self.charms = Annotations(self.env)
def setUp(self): self.env = Environment.connect(ENV_NAME) self.charms = Charms(self.env)
def setUp(self): self.env = Environment.connect(ENV_NAME) self.actions = Actions(self.env) self.setupCharm()
def setUp(self): self.env = Environment.connect(ENV_NAME) self.um = UserManager(self.env)
import json from types import * from jujuclient import Environment env = Environment.connect('local') ############################################################# # General Environment Functions # Assuming: env = Environment.connect('local') ############################################################# def env_facades(): print('******************** Facades\n') obj=env.facades assert type(obj) is DictType, "obj is not a dictionary" print(obj) return def env_users(): print('\n\n******************** Users\n') obj=env.users.list() assert type(obj) is DictType, "obj is not a dictionary" print(obj) return def env_charms(): print('\n\n******************** Charms\n') obj=env.charms.list() assert type(obj) is DictType, "obj is not a dictionary"
""" self.logger.info("Destroying the machine (%s)", machine_numbers) return self.client.destroy_machines(machine_numbers, force=True) def status(self): """ Get the status of Juju :return: status of Juju as a JSON file """ self.logger.info("Get the status of the system") return self.client.status() def getserviceconfiguration(self, service_name): return self.client.get_service(service_name) def info(self): return self.client.info() if __name__ == '__main__': address = "10.0.3.1" port = 17070 token = "d36784462a74d471e34176613652cb15" print "env login" environment = Environment("wss://" + address + ":" + str(port)) environment.login(token) print "env logged" # print environment.add_local_charm("/home/ubuntu/charms/trusty/torquepbs2.zip", "trusty") print environment.deploy("apache2", "local:trusty/torquepbs-1")