fgew2 发表于 2015-4-17 09:23:08

pupput加入git版本控制线上环境部署及使用说明

                      注:本文侧重于pupput使用git版本控制在线上环境的部署步骤详解。
1:线上puppet加入git版本控制
详细步骤:

1
2
3
4
5
6
7
8
9
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com
root@pts/0 # git init ; cd ..; git clone --bar environment /var/puppet/environment.git
Initialized empty Git repository in /etc/puppet/multi-environment/yydev/environment/.git/
Initialized empty Git repository in /var/puppet/environment.git/
warning: You appear to have cloned an empty repository.
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com
root@pts/0 # git init
Initialized empty Git repository in /etc/puppet/multi-environment/yydev/application/.git/
warning: You appear to have cloned an empty repository.





将项目添加到git:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com 2015-04-15 17:37:53
root@pts/0 # git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       modules/
nothing added to commit but untracked files present (use "git add" to track)
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com 2015-04-15 17:37:56
root@pts/0 # git add modules
root@pts/0 # git commit -a -m "init repo"
init repo
Committer: root <root@pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

    git config --global user.name "Your Name"
    git config --global user.email you@example.com

If the identity used for this commit is wrong, you can fix it with:

    git commit --amend --author='Your Name <you@example.com>'

2 files changed, 24 insertions(+), 0 deletions(-)
create mode 100644 modules/httpd/files/1.txt
create mode 100644 modules/httpd/manifests/init.pp





配置提交信息:

1
2
3
4
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com 2015-04-15 17:38:47
root@pts/0 # git config --global user.name "wangyanliang"
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com 2015-04-15 17:39:10
root@pts/0 # git config --global user.email "wangyl@youyuan.com"





配置git远程节点名称:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com 2015-04-15 17:51:10
root@pts/0 # git remote add origin /var/puppet/environment.git
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com 2015-04-15 17:52:17
root@pts/0 # cd ../application
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com 2015-04-15 17:52:35
root@pts/0 # git remote add app_origin /var/puppet/application.git
root@pts/0 # git remote -v
app_origin      /var/puppet/application.git (fetch)
app_origin      /var/puppet/application.git (push)
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com 2015-04-15 17:53:36
root@pts/0 # cd ../environment/; git remote -v
origin/var/puppet/environment.git (fetch)
origin/var/puppet/environment.git (push)
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com 2015-04-15 17:57:08
root@pts/0 # git remote rm origin
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com 2015-04-15 17:57:18
root@pts/0 # git remote add env_origin /var/puppet/environment.git
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com 2015-04-15 17:57:34
root@pts/0 # git remote -v
env_origin      /var/puppet/environment.git (fetch)
env_origin      /var/puppet/environment.git (push)





将更改推送到git仓库:

1
2
3
4
5
6
7
8
9
10
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com 2015-04-15 17:59:05
root@pts/0 # git push app_origin master:master
Counting objects: 8, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (8/8), 705 bytes, done.
Total 8 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (8/8), done.
To /var/puppet/application.git
*       master -> master





操作yytest目录:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2015-04-15 18:00:55
root@pts/0 # git clone /var/puppet/environment.git environment
fatal: destination path 'environment' already exists and is not an empty directory.
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com 2015-04-15 18:01:10
root@pts/0 # rm -rf environment
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com 2015-04-15 18:01:38
root@pts/0 # git clone /var/puppet/environment.git environment
Initialized empty Git repository in /etc/puppet/multi-environment/yytest/environment/.git/root@pts/0 # git clone /var/puppet/application.git application
Initialized empty Git repository in /etc/puppet/multi-environment/yytest/application/.git/
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com 2015-04-15 18:02:56
root@pts/0 # cd environment/modules/ ;ls
sshtest
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com 2015-04-16 12:52:53
root@pts/0 # git remote rm origin
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com 2015-04-16 12:53:02
root@pts/0 # git remote add env_origin /var/puppet/environment.git
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com 2015-04-16 12:53:23
root@pts/0 # cd ../application ; git remote rm origin; git remote add app_origin /var/puppet/application.git
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com 2015-04-16 12:53:54
root@pts/0 # git remote -v
app_origin      /var/puppet/application.git (fetch)
app_origin      /var/puppet/application.git (push)




同样的操作yyprd目录:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com
root@pts/0 # git clone /var/puppet/application.git application
Initialized empty Git repository in /etc/puppet/multi-environment/yyprd/application/.git/
root@pts/0 # git remote rm origin
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com 2015-04-16 12:48:31
root@pts/0 # git remote add app_origin /var/puppet/application.git
root@pts/0 # git clone /var/puppet/environment.git environment
Initialized empty Git repository in /etc/puppet/multi-environment/yyprd/environment/.git/
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com 2015-04-16 11:54:32
root@pts/0 # ls environment/modules/
sshtest
root@pts/0 # git remote rm origin
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com 2015-04-16 12:49:19
root@pts/0 # git remote add env_origin /var/puppet/environment.git






至此puppet线上部署完成:多环境配置统一,并加入git,包含开发,测试,生产。让我看看我们这个环境是怎么协同工作的吧!

2:协同开发
我们假设用户user1,user2
user1负责写crontab模块。
user2负责写httpd模块。

usr1的实施步骤:
进入开发环境建git开发特性分支 env/crontab
git branch env/crontab

1
2
3
4
5
6
7
8
9
10
11
root@pts/0 # git branch
* master
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com 2015-04-16 13:39:50
root@pts/0 # git branch env/crontab
root@pts/0 # git branch -v
env/crontab c10ee1d init env
* master      c10ee1d init env
root@pts/0 # git checkout env/crontab
Switched to branch 'env/crontab'
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com 2015-04-16 13:42:55
root@pts/0 # mkdir cron/{files,manifests,templates} -p





模块命令已经写好:(虽然错误百出,哈哈, 正确的在下面)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
root@pts/0 # catinit.pp install.pp service.pp crontab.pp
class cron {
      include cron::install,cron::service,cron::crontab
}
class cron::install {
      package { "crontabs":
      ensure => present,
      }
}
class cron::service {
      service { "crond":
                ensure => running,
}
class cron::crontab {
    cron {"ntp time":
      ensure =>   present,
      command =>   "/sbin/ntpdate 192.168.0.2",
      user =>   'root',
      minute => 0,
      hour =>   '6-13/3',
      month =>   *;
}





提交代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
root@pts/0 # ls
cronsshtest
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com 2015-04-16 14:27:21
root@pts/0 # git add *
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com 2015-04-16 14:27:29
root@pts/0 # git status
# On branch env/cron
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       new file:   cron/manifests/crontab.pp
#       new file:   cron/manifests/init.pp
#       new file:   cron/manifests/install.pp
#       new file:   cron/manifests/service.pp
#
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com 2015-04-16 14:27:46
root@pts/0 # git commit -m "add cron" .   
add cron
4 files changed, 20 insertions(+), 0 deletions(-)
create mode 100644 modules/cron/manifests/crontab.pp
create mode 100644 modules/cron/manifests/init.pp
create mode 100644 modules/cron/manifests/install.pp
create mode 100644 modules/cron/manifests/service.pp




特性分支合并到主分支:

1
2
3
4
5
6
7
root@pts/0 # git checkout master
Switched to branch 'master'
root@pts/0 # git merge env/cron
Already up-to-date.
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com 2015-04-16 14:35:52
root@pts/0 # ls
cronsshtest




删除特性分支:
1
2
3
4
5
6
root@pts/0 # git branch -v
env/cron beb0b03 modify service.pp
* master   4f5ff10 modify crontab
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com 2015-04-16 15:03:49
root@pts/0 # git branch -d env/cron
Deleted branch env/cron (was beb0b03).




开发环境测试:
1: 配置开发测试的主机:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com 2015-04-16 14:22:43
root@pts/0 # ls
applicationenvironmentmanifests
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com 2015-04-16 14:22:43
root@pts/0 # cd manifests/
nodes/   site.pp
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com 2015-04-16 14:22:43
root@pts/0 # cat manifests/site.pp
import "nodes/*.pp"
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com 2015-04-16 14:22:56
root@pts/0 # cat manifests/nodes/test.pp
class base {
      include ssh
      include cron
}
node 'ppttest_wangsu_bj_192.168.3.67.centos.yypuppet.com' {
      include base
      include test
      include httpd
}





2: 开发测试主机上测试:
出错1:

1
2
3
4
root@pts/0 # puppet agent -t --environment=yydev--noop --no-daemonize --verbose
err: Could not retrieve catalog from remote server: Error 400 on SERVER: Could not find class cron for ppttest_wangsu_bj_192.168.3.67.centos.yypuppet.com at /etc/puppet/multi-environment/yydev/manifests/nodes/test.pp:3 on node ppttest_wangsu_bj_192.168.3.67.centos.yypuppet.com
warning: Not using cache on failed catalog
err: Could not retrieve catalog; skipping run




错误排查:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
root@pts/0 # git checkout master
Switched to branch 'master'
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com 2015-04-16 14:42:44
root@pts/0 # ls
filestemplates
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com 2015-04-16 14:42:44
root@pts/0 # git checkout env/cron
Switched to branch 'env/cron'
root@pts/0 # git merge master
Already up-to-date.
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com 2015-04-16 14:43:40
root@pts/0 # git checkout env/cron
Already on 'env/cron'
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com 2015-04-16 14:43:49
root@pts/0 # ls
filesmanifeststemplates




出错2:

1
2
3
4
root@pts/0 # puppet agent -t --environment=yydev--noop --no-daemonize --verbose
err: Could not retrieve catalog from remote server: Error 400 on SERVER: Syntax error at end of file; expected '}' at /etc/puppet/multi-environment/yydev/environment/modules/cron/manifests/service.pp:5 on node ppttest_wangsu_bj_192.168.3.67.centos.yypuppet.com
warning: Not using cache on failed catalog
err: Could not retrieve catalog; skipping run




报错排查,可见是语法错误:
service.pp少了个“}”,再次修复。

开发测试机成功。

1
2
3
4
5
6
7
root@pts/0 # puppet agent -t --environment=yydev--noop --no-daemonize --verbose
info: Caching catalog for ppttest_wangsu_bj_192.168.3.67.centos.yypuppet.com
info: Applying configuration version '1429167566'
notice: /Stage/Cron::Crontab/Cron/ensure: current_value absent, should be present (noop)
notice: Class: Would have triggered 'refresh' from 1 events
notice: Stage: Would have triggered 'refresh' from 1 events
notice: Finished catalog run in 1.07 seconds




2:测试:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
root@pts/0 # crontab -l
ppttest_wangsu_bj_192.168.3.67.centos.yypuppet.com [~] 2015-04-16 15:10:57
root@pts/0 # puppet agent -t --environment=yydev --no-daemonize --verbose
info: Caching catalog for ppttest_wangsu_bj_192.168.3.67.centos.yypuppet.com
info: Applying configuration version '1429168261'
notice: /Stage/Cron::Crontab/Cron/ensure: created
notice: Finished catalog run in 1.10 seconds
ppttest_wangsu_bj_192.168.3.67.centos.yypuppet.com [~] 2015-04-16 15:11:04
root@pts/0 # crontab -l
# HEADER: This file was autogenerated at Thu Apr 16 15:11:03 +0800 2015 by puppet.
# HEADER: While it can still be managed manually, it is definitely not recommended.
# HEADER: Note particularly that the comments starting with 'Puppet Name' should
# HEADER: not be deleted, as doing so could cause duplicate cron jobs.
# Puppet Name: ntp time
30 */2 * * * /sbin/ntpdate 192.168.0.2




提交代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
root@pts/0 # git status
# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   crontab.pp
#
no changes added to commit (use "git add" and/or "git commit -a")
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com 2015-04-16 15:01:04
root@pts/0 # git commit -a -m "modify crontab"
modify crontab
1 files changed, 7 insertions(+), 6 deletions(-)




最终版本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
root@pts/0 # cat init.pp install.pp service.pp crontab.pp
class cron {
      include cron::install,cron::service,cron::crontab
}
class cron::install {
      package { "crontabs":
      ensure => present,
      }
}
class cron::service {
      service { "crond":
                ensure => running,
}
}
class cron::crontab {
      cron {"ntp time":
      ensure => present,
      command =>"/sbin/ntpdate 192.168.0.2",
      user => 'root',
      minute => 30,
      hour => '*/2',
}
}





提交代码到仓库中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com 2015-04-16 15:33:36
root@pts/0 # ls
cronsshtest
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com 2015-04-16 15:33:36
root@pts/0 # git remote -v
env_origin      /var/puppet/environment.git (fetch)
env_origin      /var/puppet/environment.git (push)
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com 2015-04-16 15:33:52
root@pts/0 # git push env_origin
Counting objects: 36, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (24/24), done.
Writing objects: 100% (34/34), 2.84 KiB, done.
Total 34 (delta 6), reused 0 (delta 0)
Unpacking objects: 100% (34/34), done.
To /var/puppet/environment.git
   c10ee1d..112c636master -> master





yytest环境更新本地库:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
root@pts/0 # git branch
* master
pptserver_wangsu_bj_192.168.3.68.centos.yypuppet.com 2015-04-16 15:40:11
root@pts/0 # git pull env_origin master
From /var/puppet/environment
* branch            master   -> FETCH_HEAD
Updating c10ee1d..112c636
Fast-forward
modules/cron/manifests/crontab.pp |    9 +++++++++
modules/cron/manifests/init.pp    |    3 +++
modules/cron/manifests/install.pp |    5 +++++
modules/cron/manifests/service.pp |    5 +++++
4 files changed, 22 insertions(+), 0 deletions(-)
create mode 100644 modules/cron/manifests/crontab.pp
create mode 100644 modules/cron/manifests/init.pp
create mode 100644 modules/cron/manifests/install.pp
create mode 100644 modules/cron/manifests/service.pp




user1的cron开发完毕!

让我们来总结下这个过程:
1:user1建立一个特性分支env/cron。
2:user1在env/cron中写cron的代码,此时与主干分支是完全隔离的。
3:代码书写完毕,合并到master分支。
4:此时env/cron便可以删除了。
5:开发环境测试。修复cron的代码。
6:开发测试环境测试成功后,提交cron的代码到仓库中。
7: 测试环境测试及线上环境进行上线。

user2的实施步骤和user1步骤一致:
1:进入开发环境建立git开发分支 app/httpd
。。。
。。。

puppet模板文档:
http://docs.puppetlabs.com/references/latest/type.html#file
                   
                       
                         


   
页: [1]
查看完整版本: pupput加入git版本控制线上环境部署及使用说明