Class: Chulai::Base

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

Constant Summary collapse

API_BASE_URL =
"http://wo.chulai.la"
GIT_ROOT =
"git.chulai.la"
SSH_KEY =
"chulai"
SUFFIX =
"chulai.la"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root = '.') ⇒ Base

Returns a new instance of Base.



12
13
14
15
16
17
# File 'lib/chulai/base.rb', line 12

def initialize root = '.'
  @root = root
rescue => e
  puts "Error: #{e.inspect}"
  exit
end

Instance Attribute Details

#identityObject (readonly)

Returns the value of attribute identity.



5
6
7
# File 'lib/chulai/base.rb', line 5

def identity
  @identity
end

#tokenObject (readonly)

Returns the value of attribute token.



5
6
7
# File 'lib/chulai/base.rb', line 5

def token
  @token
end

Instance Method Details

#accountObject



19
20
21
22
23
24
25
# File 'lib/chulai/base.rb', line 19

def 
  if Git.global_config.keys.include? 'chulai.token'
    
  else
    
  end
end

#account_checkObject



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/chulai/base.rb', line 49

def 
  puts "checking account"
  @auth_token = Git.global_config 'chulai.token'
  res = http_without_auth :post, "/account_check.json", auth_token: @auth_token
  @username = res["username"]
  if res["status"] == 'success'
    [@username, @auth_token]
  else
    puts "error: #{res["message"]}"
    exit
  end
end

#account_sign_inObject



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

def 
  puts "please sign in"
  print "email: "
  email = $stdin.gets.chomp
  print "password: "
  password = STDIN.noecho(&:gets).chomp
  puts

  res = http_without_auth :post, "/account_sign_in.json", user: {
    email: email,
    password: password
  }
  if res["status"] == 'success'
    @username = res["username"]
    @auth_token = res["auth_token"]
    Git.global_config 'chulai.token', @auth_token
  else
    puts "error: #{res["message"]}"
  end
end

#auth_tokenObject



27
28
29
30
31
32
33
34
# File 'lib/chulai/base.rb', line 27

def auth_token
  if @auth_token
    @auth_token
  else
    
  end
  @auth_token
end

#deployObject



162
163
164
165
166
167
168
169
170
171
# File 'lib/chulai/base.rb', line 162

def deploy
  puts "deploying"

  stream :post, "/deploy.stream", identity: identity, commit: git.log.first.sha , comment: git.log.first.message, gems: @gems do |chunk|
    puts chunk
  end
rescue => exc
  puts "deploy failed: #{exc.to_s}"
  exit
end

#gemfileObject



122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/chulai/base.rb', line 122

def gemfile
  puts "checking Gemfile"
  unless File.exist? 'Gemfile'
    puts "Error: Gemfile not found, please make sure you are in a Rails project dir"
    exit
  end
  @gemfile = Gemnasium::Parser::Gemfile.new File.read("Gemfile")
  @gems = @gemfile.dependencies.map(&:name)
  unless @gems.include? 'rails'
    puts "Error: chulai ONLY support Rails projects by now"
    exit
  end
end

#gitObject



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/chulai/base.rb', line 36

def git
  return @git if @git
  @git = Git.open @root#, log: Logger.new(STDOUT)
  unless @git.log.first.to_s == @git.branches[:master].gcommit.log.first.to_s
    puts "Error: please checkout to master branch first"
    exit
  end
  @git
rescue => e
  puts "Error: git not initailzed"
  exit
end

#openObject



173
174
175
176
177
178
179
180
# File 'lib/chulai/base.rb', line 173

def open
  url = "http://#{@username}.#{@name}.#{SUFFIX}"
  Launchy.open url
rescue => exc
  puts "failed to launch any browser: #{exc.to_s}"
  puts url
  exit
end

#pushObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/chulai/base.rb', line 136

def push
  puts "pushing to git server"

  remote = git.remote(:chulai)
  if remote.url.nil?
    res = http :post, "/birth.json"

    raise res.inspect unless res['status'] == 'success'
    @identity, @name = res["identity"], res["name"]
    git.add_remote :chulai, "#{SSH_KEY}:#{@identity}.git"
    git.config 'chulai.identity', @identity
    git.config 'chulai.name', @name
  else
    @identity = git.config 'chulai.identity'

    res = http :post, "/check.json", identity: @identity

    raise res.inspect unless res && res['status'] == 'success'
    @name = res["name"]
  end
  git.push(git.remote("chulai"))
rescue => exc
  puts "failed to push code to git server: #{exc.to_s}"
  exit
end

#ssh_configObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/chulai/base.rb', line 102

def ssh_config
  puts "checking ssh config"
  config_dir = File.expand_path "~/.ssh"
  Dir.mkdir config_dir unless Dir.exist? config_dir

  config_path = File.join config_dir, "config"
  unless File.exist?(config_path) and Net::SSH::Config.load(config_path, SSH_KEY).length > 1
    File.open config_path, "a" do |f|
      f.write [
        "",
        "Host #{SSH_KEY}",
        "HostName #{GIT_ROOT}",
        "User git",
        "IdentityFile ~/.ssh/#{SSH_KEY}",
        "",
      ].join("\n")
    end
  end
end

#ssh_keyObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/chulai/base.rb', line 83

def ssh_key
  puts "checking ssh key"
  key_dir = File.expand_path "~/.ssh"
  Dir.mkdir key_dir unless Dir.exist? key_dir
  key_path = File.join key_dir, SSH_KEY
  key = SSHKey.new File.read key_path
rescue => e
  puts "sshkey ~/.ssh/chulai not found, generate a new one"
  key = SSHKey.generate
  File.open(key_path, "w") do |f|
    f.write key.private_key
    f.chmod 0400
  end
  File.open("#{key_path}.pub", "w") do |f|
    f.write key.ssh_public_key
  end
  res = http :post, "/keys.json", key: {name: Socket.gethostname, content: key.ssh_public_key}
end