Class: Orats::Commands::Common

Inherits:
Object
  • Object
show all
Includes:
Diff::Parse, UI, Thor::Actions, Thor::Base, Thor::Shell
Defined in:
lib/orats/commands/common.rb

Direct Known Subclasses

Diff::Exec, Inventory, Nuke, Playbook, Project::Exec, Role

Constant Summary collapse

RELATIVE_PATHS =
{
    galaxyfile: 'templates/includes/playbook/Galaxyfile',
    playbook:   'templates/includes/playbook/site.yml',
    hosts:      'templates/includes/inventory/hosts',
    inventory:  'templates/includes/inventory/group_vars/all.yml',
    version:    'version.rb'
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Diff::Parse

#galaxyfile, #gem_version, #hosts, #inventory, #playbook

Methods included from UI

#git_commit, #log_error, #log_remote_info, #log_results, #log_status_bottom, #log_status_top, #log_task, #run_from

Constructor Details

#initialize(target_path = '', options = {}) ⇒ Common

Returns a new instance of Common.



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/orats/commands/common.rb', line 23

def initialize(target_path = '', options = {})
  @target_path = target_path
  @options     = options
  @active_path = @target_path

  @local_paths  = {}
  @remote_paths = {}

  build_common_paths

  self.destination_root = Dir.pwd
  @behavior             = :invoke
end

Instance Attribute Details

#local_pathsObject

Returns the value of attribute local_paths.



21
22
23
# File 'lib/orats/commands/common.rb', line 21

def local_paths
  @local_paths
end

#remote_gem_versionObject

Returns the value of attribute remote_gem_version.



21
22
23
# File 'lib/orats/commands/common.rb', line 21

def remote_gem_version
  @remote_gem_version
end

#remote_pathsObject

Returns the value of attribute remote_paths.



21
22
23
# File 'lib/orats/commands/common.rb', line 21

def remote_paths
  @remote_paths
end

Class Method Details

.copy_from_local_gem(source, dest) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/orats/commands/common.rb', line 37

def self.copy_from_local_gem(source, dest)
  base_path     = File.join(File.expand_path(File.dirname(__FILE__)),
                            '..')
  template_path = "#{base_path}/templates/includes"

  system "mkdir -p #{File.dirname(dest)}" unless Dir.exist?(File.dirname(dest))
  system "cp #{template_path}/#{source} #{dest}"
end

Instance Method Details

#base_pathObject



46
47
48
# File 'lib/orats/commands/common.rb', line 46

def base_path
  File.join(File.expand_path(File.dirname(__FILE__)), '..')
end

#exit_if_path_exists(extend_path = '') ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/orats/commands/common.rb', line 74

def exit_if_path_exists(extend_path='')
  log_task 'Check if this path exists'

  extended_path = @active_path.dup
  extended_path = File.join(extended_path, extend_path) unless extend_path
  .empty?

  if Dir.exist?(extended_path) || File.exist?(extended_path)
    log_error 'error', 'A file or directory already exists at this location', 'path', extended_path
    exit 1
  end
end

#exit_if_process(check_for, *processes) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/orats/commands/common.rb', line 87

def exit_if_process(check_for, *processes)
  case check_for
    when :not_found
      command = 'which'
      phrase  = 'on your system path'
    when :not_running
      command = 'ps cax | grep'
      phrase  = 'running'
    else
      command = ''
      phrase  = ''
  end

  processes.each do |process|
    log_task "Check if #{process} is #{phrase}"

    exit 1 if process_unusable?("#{command} #{process}", process, phrase)
  end
end

#file_to_string(path) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/orats/commands/common.rb', line 64

def file_to_string(path)
  if File.exist?(path) && File.file?(path)
    IO.read(path)
  else
    log_error 'error', 'Error finding file',
              'message', path
    exit 1
  end
end

#repo_pathObject



50
51
52
# File 'lib/orats/commands/common.rb', line 50

def repo_path
  %w(https://raw.githubusercontent.com/nickjj/orats lib/orats)
end

#url_to_string(url) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/orats/commands/common.rb', line 54

def url_to_string(url)
  begin
    open(url).read
  rescue *[OpenURI::HTTPError, SocketError] => ex
    log_error 'error', "Error accessing URL #{url}",
              'message', ex
    exit 1
  end
end