Class: Terrafying::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/terrafying.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, options) ⇒ Config

Returns a new instance of Config.



22
23
24
25
26
27
28
29
30
# File 'lib/terrafying.rb', line 22

def initialize(path, options)
  @path = File.expand_path(path)
  @options = options
  @scope = options[:scope] || scope_for_path(@path)

  $stderr.puts "Scope: #{@scope}"

  load(path)
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



20
21
22
# File 'lib/terrafying.rb', line 20

def path
  @path
end

#scopeObject (readonly)

Returns the value of attribute scope.



20
21
22
# File 'lib/terrafying.rb', line 20

def scope
  @scope
end

Instance Method Details

#applyObject



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/terrafying.rb', line 70

def apply
  exit_code = 1
  with_config do
    with_lock do
      with_state(mode: :update) do
        exit_code = exec_with_optional_target "apply -auto-approve -backup=- #{@dir}"
      end
    end
  end
  exit_code
end

#destroyObject



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/terrafying.rb', line 82

def destroy
  exit_code = 1
  with_config do
    with_lock do
      with_state(mode: :update) do
        exit_code = stream_command("terraform destroy -backup=- #{@dir}")
      end
    end
  end
  exit_code
end

#graphObject



50
51
52
53
54
55
56
57
58
# File 'lib/terrafying.rb', line 50

def graph
  exit_code = 1
  with_config do
    with_state(mode: :read) do
      exit_code = exec_with_optional_target 'graph'
    end
  end
  exit_code
end

#import(addr, id) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/terrafying.rb', line 119

def import(addr, id)
  exit_code = 1
  with_config do
    with_lock do
      with_state(mode: :update) do
        exit_code = exec_with_optional_target "import  -backup=- #{@dir} #{addr} #{id}"
      end
    end
  end
  exit_code
end

#jsonObject



36
37
38
# File 'lib/terrafying.rb', line 36

def json
  Terrafying::Generator.pretty_generate
end

#listObject



32
33
34
# File 'lib/terrafying.rb', line 32

def list
  Terrafying::Generator.resource_names
end

#planObject



40
41
42
43
44
45
46
47
48
# File 'lib/terrafying.rb', line 40

def plan
  exit_code = 1
  with_config do
    with_state(mode: :read) do
      exit_code = exec_with_optional_target 'plan'
    end
  end
  exit_code
end

#show_stateObject



94
95
96
# File 'lib/terrafying.rb', line 94

def show_state
  puts(State.store(self).get)
end

#use_local_stateObject



109
110
111
112
113
114
115
116
117
# File 'lib/terrafying.rb', line 109

def use_local_state
  with_lock do
    remote = State.remote(self)
    state = remote.get
    if state
      State.local(self).put(state)
    end
  end
end

#use_remote_stateObject



98
99
100
101
102
103
104
105
106
107
# File 'lib/terrafying.rb', line 98

def use_remote_state
  with_lock do
    local = State.local(self)
    state = local.get
    if state
      State.remote(self).put(state)
    end
    local.delete
  end
end

#validateObject



60
61
62
63
64
65
66
67
68
# File 'lib/terrafying.rb', line 60

def validate
  exit_code = 1
  with_config do
    with_state(mode: :read) do
      exit_code = exec_with_optional_target 'validate'
    end
  end
  exit_code
end