fabfile.py
๋ฐ tasks.py
๋ก ์ ์ฅ๋์ด ์์
์ ์๋ฅผ ๊ณ ๋ คํ์ญ์์ค.
from invoke import task
<strong i="8">@task</strong>
def make(c):
c.run('env')
fab make
์ถ๋ ฅ :
PWD=/tmp
SHLVL=1
_=/usr/bin/env
inv make
์ถ๋ ฅ :
LC_ALL=en_US.UTF-8
NVM_DIR=/home/justinas/.nvm
LC_MEASUREMENT=en_US.UTF-8
LC_PAPER=en_US.UTF-8
LC_MONETARY=en_US.UTF-8
<...> (lots of stuff in my environment)
์ฒ์์๋ PATH
๋ณ์๋ฅผ ํ์ธํ์ฌ ์๋ํ๋ ๋ฐ ํ์ํ ์คํ ํ์ผ์ด ์กด์ฌํ๋์ง ํ์ธํ๋ fpm ์ ์ฌ์ฉํ๋ ค๊ณ ํ ๋ ์ด๊ฒ์ ๋ฐ๊ฒฌํ์ต๋๋ค.
from invoke import task
<strong i="22">@task</strong>
def make(c):
c.run('fpm -s dir -t rpm -C dist --name somename .')
์ด๊ฒ์ invoke์ ํจ๊ป ์๋ํ์ง๋ง fab์์๋ ์๋ํ์ง ์์ต๋๋ค.
justinas<strong i="26">@js</strong>:/tmp$ fab make
{:timestamp=>"2018-05-11T13:11:26.218874+0300", :message=>"Need executable 'rpmbuild' to convert dir to rpm", :level=>:error}
justinas<strong i="27">@js</strong>:/tmp$ inv make
{:timestamp=>"2018-05-11T13:11:29.967762+0300", :message=>"Created package", :path=>"somename-1.0-1.x86_64.rpm"}
์ด ํ๋์ ๋ํ ์ค๋ช ์ ๋ฐ๊ฒฌ
์ด๊ฒ์ ์๊ฒฉ ์์คํ ์์ ๋ช ๋ น์ ์คํํ๋ ๋ฐ ์ผ๋ฐ์ ์ ๋๋ค.
$ env | wc -l
41
$ ssh testdeploy01.ec2.st-av.net env | wc -l
14
$ ssh testdeploy01.ec2.st-av.net grep Env /etc/ssh/sshd_config
AcceptEnv LANG LC_*
Specifies what environment variables sent by the client will be copied into
the session's environ(7). See SendEnv in ssh_config(5) for how to configure
the client. The TERM environment variable is always sent whenever the
client requests a pseudo-terminal as it is required by the protocol. Vari-
ables are specified by name, which may contain the wildcard characters `*'
and `?'. Multiple environment variables may be separated by whitespace or
spread across multiple AcceptEnv directives. Be warned that some environ-
ment variables could be used to bypass restricted user environments. For
this reason, care should be taken in the use of this directive. The default
is not to accept any environment variables.
์ ์ฒด ๋ก์ปฌ ํ๊ฒฝ์ ์์งํ๊ณ ๋ค๋ฅธ์ชฝ์ ์ค์ ํ์ฌ ๋ชจ๋ ํ๊ฒฝ ๋ณ์๋ฅผ ํต๊ณผํ๋ ค๊ณ ์๋ ํ ์ ์์ง๋ง PATH
์ ๊ฐ์ ์๊ฒฉ ์์คํ
์ ๋ํ ์ ์ ํ ๊ฐ๊ณผ ์ถฉ๋ ํ ์ ์๊ธฐ ๋๋ฌธ์ ๋ฌด์ํด์ผํ๋ ๋ง์ ๋ณ์๊ฐ ์์ต๋๋ค. GOPATH
(Go ์ธ์ด๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ), TMPDIR
, HOME
, SSH_AUTH_SOCK
, ...
๋๋ ์ ์ ์ผ๋ก ๋ก์ปฌ ์์ ์ ๋ํด์๋ง ์ด์ผ๊ธฐํ๊ณ ์์ต๋๋ค. ๋ก์ปฌ ๋ช ๋ น์ ๋ํด ์ ๋ฌ ํ๊ฒฝ์ด ๊ธฐ๋ณธ์ ์ผ๋ก ์ผ์ ธ ์์ด์ผ ํจ์ ํ์ธ ํ๋ TODO ์ฃผ์ ๋ ๋ฐ๊ฒฌ
์, ์๊ฒ ์ต๋๋ค.
@justinas ๋ก์ปฌ ์์ ์ ๋ํด ํธ์ถ์์ ์คํ์ ์ฌ์ฉํ์ฌ ๋๋ฌ
from invoke import run as local
<strong i="7">@task</strong>
def test(c):
local('ls')
์ด๊ฒ์ # 1752์ ํจ์ฏ์
๋๋ค-์๋๋ local
๊ฐ Invoke์ฒ๋ผ ์๋ํ๊ณ env๋ฅผ ๋ณด์กดํ๊ณ run
๊ฐ SSH์ฒ๋ผ ์ ๋๋ก ์๋ํ๊ณ env๋ฅผ ๋ฒ๋ฆฌ๋ ๊ฒ์
๋๋ค. ๊ทธ๋ฌ๋ ์ง๊ธ์์ด ๋์์ ์ ๋ํ๋ ๊ตฌ์ฑ์ด ์ ๋๋ก ๋ถ๋ฆฌ๋์ง ์์์ต๋๋ค. ์ด๋ ์ฌ์ค์ ๋ฒ๊ทธ์
๋๋ค. ๊ณง ์์ ํ๊ฒ ์ต๋๋ค.
์๋ต ํด ์ฃผ์ ์ ๊ฐ์ฌํฉ๋๋ค!
๋๋ ๋ฐฉ๊ธ ๊ฐ์ ๋ฌธ์ ์ ๋ถ๋ช ํ๊ณ ๋ฒ๋ ค์ง๋ ๊ฒ์ ์ง์ญ ํ๊ฒฝ ๋ฟ๋ง์ด ์๋๋ผ๋ ์ ์ ์ฃผ๋ชฉํ๊ณ ์ถ์์ต๋๋ค.
์ด ๋ฒ๊ทธ๋ ์ด๋ฏธ ์๊ณ ์์ ๊ฒ์ ๋๋ค.
๋ช ํํํ๊ธฐ ์ํด ์ด๊ฒ์ ๋๋ฅผ ์ํด ์๋ํ์ง ์์์ต๋๋ค.
<strong i="8">@task</strong>
def test(c):
c.run('echo $ENV', env={'ENV': 'production'}) # no output
๋น๋ถ๊ฐ ๋๋ ์ฌ์ฉํ ์์๋์ด ํด๊ฒฐ ๋ฐฉ๋ฒ (๋งค์ฐ ๊นจ๋ํ์ง ์์)์ ์ฐพ์์ต๋๋ค.
<strong i="12">@task</strong>
def test(c):
with c.prefix('ENV=production'):
c.run('echo $ENV') # prints "production"
์, ์๊ณ ์์ต๋๋ค. ๊ฐ์ฌํฉ๋๋ค!
์ง๊ธ์ run('...', preserve_env=True)
๋ผ๊ณ ๋งํ์ฌ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ์ ์์ต๋๋ค. IIRC.
preserve_env
์ด ์กด์ฌํ์ง ์๋ ๊ฒ ๊ฐ์ต๋๋ค. replace_env=False
์ธ๊ธ ํ์
จ๋์?
๊ทธ๊ฒ์ ์ค๋ฅ๋ฅผ ์ ๋ฐํ์ง ์์ง๋ง ํ๊ฒฝ์ ์ฌ์ ํ โโํ๊ธฐ๋๊ณ ์์ต๋๋ค.
์ถ์ : ๋ก์ปฌ์ด ์๋ ์๊ฒฉ ์์ ์์ํ ํ๊ฒฝ์ ์ค์ ํด์ผํฉ๋๋ค.
๋ค, replace_env
์ (๋ฅผ) ์๋ฏธํ๋๋ฐ ์๋ํ์ง ์๋ ๊ฒ์ด ์ด์ํฉ๋๋ค. ๊ตฌ์ฑ์ ๋ก์ปฌ / ์๊ฒฉ (# 1752)์ผ๋ก ๋ถํ ํ ๋ ์ดํด ๋ณด๊ฒ ์ต๋๋ค.
๋๋ ์ด๊ฒ๋ ๋ง๋ฌ๋ค. ๋๋ c.run('make build')
ํ๊ณ ์์๋๋ฐ ๋ด ENV๊ฐ ํ๋ฆฌ์ง ์์๋ค (ํนํ ๋น๋ ์คํจ๋ฅผ ์ผ์ผํจ GOPATH). replace_env=False
์ถ๊ฐํ๋ฉด ๋ฌธ์ ๊ฐ ํด๊ฒฐ๋์์ต๋๋ค.
ํธ์ง : ๋๋ ๋ฌธ์ ์ ๋ฐ๋ผ ๋ณธ๋ค :
run.replace_env : False ๋์ True์ด๋ฏ๋ก ์๊ฒฉ ๋ช ๋ น์ด ํ์ฌ ํ๋ก์ธ์ค์ ๋ณต์ฌ๋ณธ์ ์์ํ๋ ๋์ '๊นจ๋ํ'๋น ํ๊ฒฝ์์ ์คํ๋ฉ๋๋ค.
๋ก์ปฌ ๋ช ๋ น์ ์คํํ๋ ค๊ณ ํ๊ธฐ ๋๋ฌธ์ ์ฝ๊ฐ ํผ๋ ์ค๋ฌ์ ์ต๋๋ค.
๋ก์ปฌ ๋ช ๋ น์ ์คํํ ๋ ๋ฌธ์ ๊ฐ ์ฌ์ ํ ์กด์ฌํฉ๋๋ค. ๋ฟก ๋นต๋จ
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
๋๋ ์ด๊ฒ๋ ๋ง๋ฌ๋ค. ๋๋
c.run('make build')
ํ๊ณ ์์๋๋ฐ ๋ด ENV๊ฐ ํ๋ฆฌ์ง ์์๋ค (ํนํ ๋น๋ ์คํจ๋ฅผ ์ผ์ผํจ GOPATH).replace_env=False
์ถ๊ฐํ๋ฉด ๋ฌธ์ ๊ฐ ํด๊ฒฐ๋์์ต๋๋ค.ํธ์ง : ๋๋ ๋ฌธ์ ์ ๋ฐ๋ผ ๋ณธ๋ค :
๋ก์ปฌ ๋ช ๋ น์ ์คํํ๋ ค๊ณ ํ๊ธฐ ๋๋ฌธ์ ์ฝ๊ฐ ํผ๋ ์ค๋ฌ์ ์ต๋๋ค.