Module: Central::Cli::Apps::Common

Instance Method Summary collapse

Methods included from Services::ServicesHelper

#create_service, #delete_service, #deploy_service, #get_service, #int_to_filesize, #parse_image, #parse_links, #parse_log_opts, #parse_memory, #parse_ports, #parse_relative_time, #parse_secrets, #parse_service_id, #restart_service, #scale_service, #show_service, #start_service, #stop_service, #update_service, #wait_for_deploy_to_finish

Methods included from Common

#access_token=, #add_master, #api_url, #api_url=, #clear_current_stack, #client, #current_master, #current_master=, #current_master_index, #current_stack, #current_stack=, #ensure_custom_ssl_ca, #require_api_url, #require_current_stack, #require_token, #reset_client, #save_settings, #settings, #settings_filename

Instance Method Details

#app_jsonHash

Returns:

  • (Hash)


126
127
128
129
130
131
132
133
# File 'lib/central/cli/apps/common.rb', line 126

def app_json
  @app_json = if !@app_json && File.exist?('app.json')
                JSON.parse(File.read('app.json'))
              else
                {}
  end
  @app_json
end

#create_yml(services, file = 'central.yml') ⇒ Object

Parameters:

  • services (Hash)
  • file (String) (defaults to: 'central.yml')


119
120
121
122
123
# File 'lib/central/cli/apps/common.rb', line 119

def create_yml(services, file = 'central.yml')
  yml = File.new(file, 'w')
  yml.puts services.to_yaml
  yml.close
end

#current_dirString

Returns:

  • (String)


36
37
38
# File 'lib/central/cli/apps/common.rb', line 36

def current_dir
  File.basename(Dir.getwd)
end

#extend_env_vars(from, to) ⇒ Array

Parameters:

  • from (Array)
  • to (Array)

Returns:

  • (Array)


94
95
96
97
98
99
100
101
102
# File 'lib/central/cli/apps/common.rb', line 94

def extend_env_vars(from, to)
  env_vars = to || []
  if from
    from.each do |env|
      env_vars << env unless to && to.find { |key| key.split('=').first == env.split('=').first }
    end
  end
  env_vars
end

#extend_options(options, file, service_name, prefix) ⇒ Hash

Parameters:

  • options (Hash)
  • file (String)
  • service_name (String)
  • prefix (String)

Returns:

  • (Hash)


77
78
79
80
81
82
# File 'lib/central/cli/apps/common.rb', line 77

def extend_options(options, file, service_name, prefix)
  parent_options = parse_services(file, service_name, prefix)
  options['environment'] = extend_env_vars(parent_options['environment'], options['environment'])
  options['secrets'] = extend_secrets(parent_options['secrets'], options['secrets'])
  parent_options.merge(options)
end

#extend_secrets(from, to) ⇒ Array

Parameters:

  • from (Array)
  • to (Array)

Returns:

  • (Array)


107
108
109
110
111
112
113
114
115
# File 'lib/central/cli/apps/common.rb', line 107

def extend_secrets(from, to)
  secrets = to || []
  if from
    from.each do |from_secret|
      secrets << from_secret unless to && to.any? { |to_secret| to_secret['secret'] == from_secret['secret'] }
    end
  end
  secrets
end

#load_services(filename, service_list, prefix) ⇒ Hash

Parameters:

  • filename (String)
  • service_list (Array<String>)
  • prefix (String)

Returns:

  • (Hash)


16
17
18
19
20
# File 'lib/central/cli/apps/common.rb', line 16

def load_services(filename, service_list, prefix)
  services = parse_services(filename, nil, prefix)
  services.delete_if { |name, _service| !service_list.include?(name) } unless service_list.empty?
  services
end

#normalize_env_vars(options) ⇒ Object

Parameters:

  • options (Hash)


85
86
87
88
89
# File 'lib/central/cli/apps/common.rb', line 85

def normalize_env_vars(options)
  if options['environment'].is_a?(Hash)
    options['environment'] = options['environment'].map { |k, v| "#{k}=#{v}" }
  end
end

#parse_services(file, name = nil, prefix = '') ⇒ Hash

Parameters:

  • file (String)
  • name (String, NilClass) (defaults to: nil)
  • prefix (String) (defaults to: '')

Returns:

  • (Hash)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/central/cli/apps/common.rb', line 52

def parse_services(file, name = nil, prefix = '')
  services = YAML.load(File.read(File.expand_path(file)) % { project: prefix, stack: current_stack })
  Dir.chdir(File.dirname(File.expand_path(file))) do
    services.each do |name, options|
      normalize_env_vars(options)
      next unless options.key?('extends')
      extension_file = options['extends']['file']
      service_name = options['extends']['service']
      options.delete('extends')
      services[name] = extend_options(options, extension_file, service_name, prefix)
    end
  end
  if name.nil?
    services
  else
    abort("Service #{name} not found in #{file}") unless services.key?(name)
    services[name]
  end
end

#prefixed_name(name) ⇒ String

Parameters:

  • name (String)

Returns:

  • (String)


29
30
31
32
33
# File 'lib/central/cli/apps/common.rb', line 29

def prefixed_name(name)
  return name if service_prefix.strip == ''

  "#{service_prefix}-#{name}"
end

#require_config_file(filename) ⇒ Object



8
9
10
# File 'lib/central/cli/apps/common.rb', line 8

def require_config_file(filename)
  abort("File #{filename} does not exist") unless File.exist?(filename)
end

#service_exists?(name) ⇒ Boolean

Parameters:

  • name (String)

Returns:

  • (Boolean)


42
43
44
45
46
# File 'lib/central/cli/apps/common.rb', line 42

def service_exists?(name)
  get_service(token, prefixed_name(name))
rescue
  false
end

#tokenString

Returns:

  • (String)


23
24
25
# File 'lib/central/cli/apps/common.rb', line 23

def token
  @token ||= require_token
end

#valid_addons(prefix = nil) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/central/cli/apps/common.rb', line 135

def valid_addons(prefix = nil)
  prefix = "#{prefix}-" if prefix

  {
    'openredis' => {
      'image' => 'redis:latest',
      'environment' => ["REDIS_URL=redis://#{prefix}openredis:6379"]
    },
    'redis' => {
      'image' => 'redis:latest',
      'environment' => ["REDIS_URL=redis://#{prefix}redis:6379"]
    },
    'rediscloud' => {
      'image' => 'redis:latest',
      'environment' => ["REDISCLOUD_URL=redis://#{prefix}rediscloud:6379"]
    },
    'postgresql' => {
      'image' => 'postgres:latest',
      'environment' => ["DATABASE_URL=postgres://#{prefix}postgres:@postgresql:5432/postgres"]
    },
    'mongolab' => {
      'image' => 'mongo:latest',
      'environment' => ["MONGOLAB_URI=#{prefix}mongolab:27017"]
    },
    'memcachedcloud' => {
      'image' => 'memcached:latest',
      'environment' => ["MEMCACHEDCLOUD_SERVERS=#{prefix}memcachedcloud:11211"]
    }
  }
end