Beispiel #1
0
 def create(self, hostname, env):
     env = {
         k: yaml.scalarstring.DoubleQuotedScalarString(v)
         for k, v in env.iteritems() if v is not None
     }
     if self.app.docker_ucr_style_env:
         env.update({shell_safe(k).upper(): v for k, v in env.iteritems()})
     else:
         env = {shell_safe(k).upper(): v for k, v in env.iteritems()}
     self._setup_env(env=env)
     self._setup_yml(recreate=True, env=env)
     ret, out_up = call_process2([
         'docker-compose', '-p', self.app.id, 'up', '-d', '--no-build',
         '--no-recreate'
     ],
                                 cwd=self.app.get_compose_dir())
     if ret != 0:
         raise DockerCouldNotStartContainer(out_up)
     self.container = self._get_main_service_container_id()
     if self.container is None:
         try:
             out_ps = ps(only_running=True)
         except Exception as e:
             out_ps = str(e)
         raise DockerCouldNotStartContainer(
             'could not find container for service %s! docker-ps: %s docker-compose: %s)'
             % (self.app.docker_main_service, out_ps, out_up))
     else:
         ucr_save({self.app.ucr_container_key: self.container})
         return self.container
Beispiel #2
0
 def ucr_filter_env_file(self, env):
     env_file = os.path.join(self.app.get_data_dir().rstrip('data'),
                             self.app.id + '.env')
     # remove old env file
     try:
         os.remove(env_file)
     except OSError:
         pass
     # create new env file
     fd = os.open(env_file, os.O_RDWR | os.O_CREAT)
     os.chmod(env_file, 0o400)
     with os.fdopen(fd, 'w') as outfile:
         # appcenter env file
         if os.path.exists(self.app.get_cache_file('env')):
             with open(self.app.get_cache_file('env'), 'r') as infile:
                 outfile.write(ucr_run_filter(infile.read()))
                 outfile.write('\n')
         # env variables from appcenter
         for key, value in env.iteritems():
             if value is None:
                 continue
             if self.app.docker_ucr_style_env:
                 outfile.write('%s=%s\n' % (shell_safe(key), value))
             outfile.write('%s=%s\n' % (shell_safe(key).upper(), value))
     return env_file
Beispiel #3
0
 def main(self, args):
     for section, key, value in self.get_values(args.app, args.keys):
         if args.shell:
             if isinstance(value, list):
                 value = ' '.join(value)
             if isinstance(value, bool):
                 value = int(value)
             if value is None:
                 value = ''
             value = str(value)
             if args.values_only:
                 self.log(value)
             else:
                 if section is not None:
                     key = '%s__%s' % (section, key)
                 self.log('%s=%s' % (shell_safe(key), quote(value)))
         else:
             if isinstance(value, list):
                 value = ', '.join(value)
             if section is not None:
                 key = '%s/%s' % (section, key)
             if args.values_only:
                 self.log(value)
             else:
                 self.log('%s: %s' % (key, value))
 def create(self, hostname, env):
     env = {
         k: yaml.scalarstring.DoubleQuotedScalarString(v)
         for k, v in env.iteritems()
     }
     env.update({shell_safe(k).upper(): v for k, v in env.iteritems()})
     self._setup_yml(recreate=True, env=env)
     call_process([
         'docker-compose', '-p', self.app.id, 'up', '-d', '--no-build',
         '--no-recreate'
     ],
                  cwd=self.app.get_compose_dir())
     try:
         out = ps(only_running=True)
     except CalledProcessError:
         return False
     else:
         yml_file = self.app.get_compose_file('docker-compose.yml')
         content = yaml.load(open(yml_file),
                             yaml.RoundTripLoader,
                             preserve_quotes=True)
         docker_image = content['services'][
             self.app.docker_main_service]['image']
         for line in out.splitlines():
             try:
                 container, image = line.split()[:2]
             except ValueError:
                 pass
             else:
                 if image == docker_image:
                     ucr_save({self.app.ucr_container_key: container})
                     self.container = container
                     return container
 def create(self, hostname, env):
     env = {
         k: yaml.scalarstring.DoubleQuotedScalarString(v)
         for k, v in env.iteritems()
     }
     if self.app.docker_ucr_style_env:
         env.update({shell_safe(k).upper(): v for k, v in env.iteritems()})
     else:
         env = {shell_safe(k).upper(): v for k, v in env.iteritems()}
     self._setup_yml(recreate=True, env=env)
     ret, out_up = call_process2([
         'docker-compose', '-p', self.app.id, 'up', '-d', '--no-build',
         '--no-recreate'
     ],
                                 cwd=self.app.get_compose_dir())
     if ret != 0:
         raise DockerCouldNotStartContainer(out_up)
     try:
         out_ps = ps(only_running=True)
     except CalledProcessError:
         out_ps = str()
     else:
         yml_file = self.app.get_compose_file('docker-compose.yml')
         content = yaml.load(open(yml_file),
                             yaml.RoundTripLoader,
                             preserve_quotes=True)
         docker_image = content['services'][
             self.app.docker_main_service]['image']
         for line in out_ps.splitlines():
             try:
                 container, image = line.split()[:2]
             except ValueError:
                 pass
             else:
                 if image == docker_image:
                     ucr_save({self.app.ucr_container_key: container})
                     self.container = container
                     return container
     if self.container is None:
         raise DockerCouldNotStartContainer(
             'could not find container for %s (image: %s) in docker-ps %s (docker-compose: %s)'
             % (self.app.docker_main_service, docker_image, out_ps, out_up))