| Conditions | 7 |
| Total Lines | 64 |
| Lines | 0 |
| Ratio | 0 % |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | # -*- coding: utf-8 -*- |
||
| 118 | def bootstrap_jenv(self, host): |
||
| 119 | """Bootstrap an environment in a sandbox. |
||
| 120 | Manual provider config keeps transient state in the form of |
||
| 121 | bootstrap-host for its config. |
||
| 122 | A temporary JUJU_HOME is used to modify environments.yaml |
||
| 123 | """ |
||
| 124 | env_name = self.config.get_env_name() |
||
| 125 | |||
| 126 | # Prep a new juju home |
||
| 127 | boot_home = os.path.join( |
||
| 128 | self.config.juju_home, "boot-%s" % env_name) |
||
| 129 | |||
| 130 | if not os.path.exists(boot_home): |
||
| 131 | os.makedirs(os.path.join(boot_home, 'environments')) |
||
| 132 | |||
| 133 | # Check that this installation has been used before. |
||
| 134 | jenv_dir = os.path.join(self.config.juju_home, 'environments') |
||
| 135 | if not os.path.exists(jenv_dir): |
||
| 136 | os.mkdir(jenv_dir) |
||
| 137 | |||
| 138 | ssh_key_dir = os.path.join(self.config.juju_home, 'ssh') |
||
| 139 | |||
| 140 | # If no keys, create juju ssh keys via side effect. |
||
| 141 | if not os.path.exists(ssh_key_dir): |
||
| 142 | self._run(["switch"]) |
||
| 143 | |||
| 144 | # Use existing juju ssh keys when bootstrapping |
||
| 145 | shutil.copytree( |
||
| 146 | ssh_key_dir, |
||
| 147 | os.path.join(boot_home, 'ssh')) |
||
| 148 | |||
| 149 | # Updated env config with the bootstrap host. |
||
| 150 | with open(self.config.get_env_conf()) as handle: |
||
| 151 | data = yaml.safe_load(handle.read()) |
||
| 152 | env_conf = data['environments'].get(env_name) |
||
| 153 | env_conf['bootstrap-host'] = host |
||
| 154 | |||
| 155 | with open(os.path.join(boot_home, 'environments.yaml'), 'w') as handle: |
||
| 156 | handle.write(yaml.safe_dump({ |
||
| 157 | 'environments': {env_name: env_conf} |
||
| 158 | })) |
||
| 159 | |||
| 160 | # Change JUJU_ENV |
||
| 161 | env = dict(os.environ) |
||
| 162 | env['JUJU_HOME'] = boot_home |
||
| 163 | env['JUJU_LOGGING'] = "<root>=DEBUG" |
||
| 164 | cmd = ['bootstrap', '--debug'] |
||
| 165 | if self.config.upload_tools: |
||
| 166 | cmd.append("--upload-tools") |
||
| 167 | cmd.append('--series') |
||
| 168 | cmd.append("%s" % (",".join(sorted(SERIES_MAP.values())))) |
||
| 169 | |||
| 170 | capture_err = self.config.verbose and True or False |
||
| 171 | try: |
||
| 172 | self._run(cmd, env=env, capture_err=capture_err) |
||
| 173 | # Copy over the jenv |
||
| 174 | shutil.copy( |
||
| 175 | os.path.join( |
||
| 176 | boot_home, "environments", "%s.jenv" % env_name), |
||
| 177 | os.path.join( |
||
| 178 | self.config.juju_home, |
||
| 179 | "environments", "%s.jenv" % env_name)) |
||
| 180 | finally: |
||
| 181 | shutil.rmtree(boot_home) |
||
| 182 |