Class: Pike::Env

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, &block) ⇒ Env

Constructor



16
17
18
19
20
21
# File 'lib/pike/env.rb', line 16

def initialize(name, &block)
	@name = name
	@variables = {}
    @lifecycle = []
	DSL::Environment.load(self, &block)
end

Instance Attribute Details

#bundler_prefixObject (readonly)

Aboslute path to the bundle executable on the remote machine



9
10
11
# File 'lib/pike/env.rb', line 9

def bundler_prefix
  @bundler_prefix
end

#lifecycleObject (readonly)

Returns the value of attribute lifecycle.



3
4
5
# File 'lib/pike/env.rb', line 3

def lifecycle
  @lifecycle
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/pike/env.rb', line 3

def name
  @name
end

#variablesObject (readonly)

Returns the value of attribute variables.



3
4
5
# File 'lib/pike/env.rb', line 3

def variables
  @variables
end

Instance Method Details

#add_task(name, params = {}) ⇒ Object

Called from the DSL to add a task to the lifecycle



28
29
30
# File 'lib/pike/env.rb', line 28

def add_task(name, params = {})
  @lifecycle << [name, params]
end

#get_var(key) ⇒ Object

Returns a variables value



114
115
116
# File 'lib/pike/env.rb', line 114

def get_var(key)
	@variables[key] || Main.get_var(key)
end

#prepareObject

Sets some internal variables



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/pike/env.rb', line 37

def prepare
    # Set the release path
    deploy_to = get_var(:deploy_to) + '/.pike/releases/'
    set_var release_path: (deploy_to.gsub('//', '/') + Time.new.to_i.to_s + '/')
    set_var root_dir: File.expand_path(Dir.pwd)
    set_var build_dir: File.expand_path("#{Dir.pwd}/.pikebuild")

    # Ensure the ssh connection works
    SSH::Connection.connect get_var(:host), get_var(:user)

    # Ensure sudo works if required
    if get_var(:use_sudo)
      SSH::Connection.try_sudo
    end

    # Get bundler path
    pre = "[[ -s '/etc/profile.d/rvm.sh' ]] && source /etc/profile.d/rvm.sh"
    @bundler_prefix = "#{pre} && "
end

#run_lifecycleObject

Run the complete lifecycle of the environment



90
91
92
93
94
95
96
# File 'lib/pike/env.rb', line 90

def run_lifecycle
	prepare

	@lifecycle.each do |task|
		run_task task[0], task[1]
	end
end

#run_single_task(name) ⇒ Object

Runs a single task from that environment



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/pike/env.rb', line 62

def run_single_task(name)
	prepare

	task_to_run = nil

    if name.to_s.eql?('setup')
      task_to_run = [name, {}]
    else
      lifecycle.each do |task|
        if task[0] == name
          task_to_run = task
          break
        end
      end
    end

    if task_to_run == nil
      Main.error("Unknown task '#{name}'")
    else
      run_task(task_to_run[0], task_to_run[1])
    end
end

#run_task(name, params) ⇒ Object

Runs a task for that environment



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/pike/env.rb', line 123

def run_task(name, params)
	Main.action "# Run task #{name}" do
		task = Main.find_task(name)

		if task
			task.run(self, params)
		else
			raise "Task #{name} not found"
		end
	end
end

#set_var(vars = {}) ⇒ Object

Called from the DSL to set variables



103
104
105
106
107
# File 'lib/pike/env.rb', line 103

def set_var(vars = {})
	vars.keys.each do |key|
		@variables[key] = vars[key]
	end
end