Class: Subspace::Commands::Base

Inherits:
Commander::Command
  • Object
show all
Includes:
Ansible
Defined in:
lib/subspace/commands/base.rb

Instance Method Summary collapse

Methods included from Ansible

#ansible_command, #ansible_playbook

Instance Method Details

#confirm_overwrite(file_path) ⇒ Object



61
62
63
64
65
# File 'lib/subspace/commands/base.rb', line 61

def confirm_overwrite(file_path)
  return true unless File.exists? file_path
  answer = ask "#{file_path} already exists. Reply 'y' to overwrite: [no] "
  return answer.downcase.start_with? "y"
end

#copy(src, dest = nil) ⇒ Object



54
55
56
57
58
59
# File 'lib/subspace/commands/base.rb', line 54

def copy(src, dest = nil)
  dest ||= src
  return unless confirm_overwrite File.join(dest_dir, dest)
  FileUtils.cp File.join(template_dir, src), File.join(dest_dir, dest)
  say "Wrote #{dest}"
end

#dest_dirObject



35
36
37
# File 'lib/subspace/commands/base.rb', line 35

def dest_dir
  "config/subspace"
end

#gem_pathObject



19
20
21
# File 'lib/subspace/commands/base.rb', line 19

def gem_path
  File.expand_path '../../../..', __FILE__
end

#inventoryObject



89
90
91
# File 'lib/subspace/commands/base.rb', line 89

def inventory
  @inventory ||= Subspace::Inventory.read("config/subspace/inventory.yml")
end

#pass_through_paramsObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/subspace/commands/base.rb', line 67

def pass_through_params
  ansible_options = []
  self.class::PASS_THROUGH_PARAMS.each do |param_name|
    x = param_name.split('-')[1..-1].map(&:upcase).join('_')
    hash_key = (param_name.gsub('-', '_') + (x == '' ? '' : "_#{x}")).to_sym
    value = @options.__hash__[hash_key]
    if value
      if param_name.length > 1
        ansible_options += ["--#{param_name}", value]
      else
        ansible_options += ["-#{param_name}", value]
      end
    end
  end

  ansible_options
end

#playbook_dirObject



11
12
13
# File 'lib/subspace/commands/base.rb', line 11

def playbook_dir
  File.join(gem_path, 'ansible', 'playbooks')
end

#project_nameObject



31
32
33
# File 'lib/subspace/commands/base.rb', line 31

def project_name
  File.basename(project_path) # TODO see above, this should probably be in a configuration somewhere
end

#project_pathObject



23
24
25
26
27
28
29
# File 'lib/subspace/commands/base.rb', line 23

def project_path
  unless File.exist?(File.join(Dir.pwd, "config", "subspace"))
    say "Subspace must be run from the project root"
    exit
  end
  Dir.pwd # TODO make sure this is correct if they for whatever reason aren't running subspace from the project root??
end

#require_configurationObject



7
8
9
# File 'lib/subspace/commands/base.rb', line 7

def require_configuration
  load "config/provision.rb"
end

#set_subspace_versionObject



85
86
87
# File 'lib/subspace/commands/base.rb', line 85

def set_subspace_version
  ENV['SUBSPACE_VERSION'] = Subspace::VERSION
end

#template(src, dest = nil, render_binding = nil) ⇒ Object



39
40
41
42
43
# File 'lib/subspace/commands/base.rb', line 39

def template(src, dest = nil, render_binding = nil)
  return unless confirm_overwrite File.join(dest_dir, dest || src)
  template! src, dest, render_binding
  say "Wrote #{dest || src}"
end

#template!(src, dest = nil, render_binding = nil) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/subspace/commands/base.rb', line 45

def template!(src, dest = nil, render_binding = nil)
  dest ||= src
  template = ERB.new File.read(File.join(template_dir, "#{src}.erb")), trim_mode: '-'
  result = template.result(render_binding || binding)


  File.write File.join(dest_dir, dest), result
end

#template_dirObject



15
16
17
# File 'lib/subspace/commands/base.rb', line 15

def template_dir
  File.join(gem_path, 'template', 'subspace')
end