Class: Rebi::InitService

Inherits:
Object
  • Object
show all
Includes:
Log
Defined in:
lib/rebi/init_service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Log

#colorize, #colorize_prefix, #error, #error_label, #h1, #h2, #h3, #h4, #hstatus, #log

Constructor Details

#initialize(stage_name, env_name) ⇒ InitService



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rebi/init_service.rb', line 9

def initialize stage_name, env_name
  if config.app_name.blank?
    config.app_name = get_appname
  end
  @stage_name = stage_name
  @env_name = env_name
  config.data[:stages] ||= {}.with_indifferent_access

  begin
    @stage = config.stage stage_name
    raise "Already exists" if @stage.keys.include? env_name
  rescue Rebi::Error::ConfigNotFound
    config.data[:stages][stage_name] = {}.with_indifferent_access
  end

  config.data[:stages][stage_name].merge!({
    env_name => ConfigEnvironment::DEFAULT_CONFIG.clone
  }.with_indifferent_access)

  @env_data = config.data[:stages][stage_name][env_name]
end

Instance Attribute Details

#env_dataObject (readonly)

Returns the value of attribute env_data.



7
8
9
# File 'lib/rebi/init_service.rb', line 7

def env_data
  @env_data
end

#env_nameObject (readonly)

Returns the value of attribute env_name.



7
8
9
# File 'lib/rebi/init_service.rb', line 7

def env_name
  @env_name
end

#stageObject (readonly)

Returns the value of attribute stage.



7
8
9
# File 'lib/rebi/init_service.rb', line 7

def stage
  @stage
end

#stage_nameObject (readonly)

Returns the value of attribute stage_name.



7
8
9
# File 'lib/rebi/init_service.rb', line 7

def stage_name
  @stage_name
end

Instance Method Details

#configObject



31
32
33
# File 'lib/rebi/init_service.rb', line 31

def config
  Rebi.config
end

#default_envnameObject



74
75
76
# File 'lib/rebi/init_service.rb', line 74

def default_envname
  "#{env_name}-#{stage_name}"
end

#ebObject



35
36
37
# File 'lib/rebi/init_service.rb', line 35

def eb
  @eb ||= Rebi.eb
end

#executeObject



39
40
41
42
43
# File 'lib/rebi/init_service.rb', line 39

def execute
  env_data.reverse_merge!({name: get_envname})
  env_data[:solution_stack_name] = get_solution_stack
  config.push_to_file
end

#get_appnameObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rebi/init_service.rb', line 45

def get_appname
  app_name = nil
  if (apps = eb.applications).present?
    idx = -1
    while idx < 0 || idx > apps.count
      apps.each.with_index do |app, idx|
        log "#{idx + 1}: #{app}"
      end
      log "0: Create new application"
      idx = ask_for_integer "Select application:"
    end
    app_name = idx > 0 ? apps[idx - 1] : nil
  end

  if app_name.blank?
    app_name = ask_for_string "Enter application name:"
  end

  app_name
end

#get_envnameObject



66
67
68
69
70
71
72
# File 'lib/rebi/init_service.rb', line 66

def get_envname
  name = ask_for_string "Enter environment name(Default: #{default_envname}):"
  name = name.chomp.gsub(/\s+/, "")

  name = name.present? ? name : default_envname
  name
end

#get_solution_stackObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/rebi/init_service.rb', line 78

def get_solution_stack
  idx = 0
  platform = nil
  version = nil
  while idx <= 0 || idx > eb.platforms.count
    eb.platforms.each.with_index do |pl, idx|
      log "#{idx + 1}: #{pl}"
    end
    idx = ask_for_integer "Select platform:"
  end

  platform = eb.platforms[idx - 1]
  versions = eb.versions_by_platform platform
  idx = versions.count <= 1 ? 1 : 0

  while idx <=0 || idx > versions.count
    versions.each.with_index do |ver, idx|
      log "#{idx + 1}: #{ver}"
    end
    idx = ask_for_integer "Select version:"
  end

  version = versions[idx - 1]

  eb.get_solution_stack platform, version
end

#log_labelObject



105
106
107
# File 'lib/rebi/init_service.rb', line 105

def log_label
  ""
end