Class: Planet::Cli::Application

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

Constant Summary collapse

PLANET_FILE =
'Planetfile'
LOCAL_DIR =
File.expand_path('..', __FILE__)

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Application

Returns a new instance of Application.



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/planet.rb', line 43

def initialize(*args)
  super

  unless initialized? then return end

  if not File.file?(File.join(Dir.pwd, PLANET_FILE))
    abort 'No Planetfile found in current folder'
  end

  @@planetfile = File.join(Dir.pwd, PLANET_FILE)
  load @@planetfile

end

Instance Method Details

#deploy(target, branch = Planet.configuration.branch) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/planet.rb', line 100

def deploy(target, branch = Planet.configuration.branch )
  uri = Planet.servers[target.to_sym].uri
  Net::SSH.start(uri.host, uri.user, :keys => [Planet.servers[target.to_sym].key] ) do |ssh|
    cmd = %{
      cd #{uri.path} && \
      git reset HEAD --hard && \
      git checkout #{branch} && \
      git pull origin #{branch} && \
      cd #{uri.path} && sh ./deploy/after_deploy.sh
    }
    ssh.exec(cmd)
  end
end

#initObject



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/planet.rb', line 60

def init

  if initialized? then 
    abort 'This projet has already been initialized with a Planetfile'
  end

  Dir.mkdir 'deploy/'
  template_dir = File.join(LOCAL_DIR, 'template')
  FileUtils.cp(File.join(template_dir, PLANET_FILE), '.')
  FileUtils.cp(File.join(template_dir, 'after_deploy.sh'), 'deploy/')
  FileUtils.chmod "u=wrx,go=rx", 'deploy/after_deploy.sh'

end

#setup(target) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/planet.rb', line 77

def setup(target)

  unless initialized? then 
    abort "This project hasn't been initialized. Run planet init to initialize and commit your changes"
  end

  uri = Planet.servers[target.to_sym].uri
  Net::SSH.start(uri.host, uri.user, :keys => [Planet.servers[target.to_sym].key] ) do |ssh|

    if Planet.configuration.key 
      cmd = %{ ssh-agent bash -c '
        ssh-add #{Planet.configuration.key } &&
        git clone #{Planet.configuration.repository} #{uri.path}' } 
    else
      cmd = %{ git clone --depth 1 #{Planet.configuration.repository} #{uri.path} }
    end
    ssh.exec(cmd)
  end
end