文档章节

fabric systemd journalctl

hyhlinux
 hyhlinux
发布于 2017/02/07 16:08
字数 1937
阅读 10
收藏 0

##fabric 部署imgtask app 1.serve (bug):gopath

2.worker

Sample usage:

#coding:utf-8
from __future__ import with_statement
from fabric.api import *
from fabric.context_managers import *
from fabric.contrib.console import confirm

import systemd
import utils
#ssh要用到的参数
#操作一致的服务器可以放在一组,同一组的执行同一套操作
env.roledefs = {
            'realserver1': ['root@192.168.9.3:22', ],
            'realserver2': ['root@192.168.9.4:22', ]
            }
env.password = 'tplinux'

#app config

app = "imgtask"
app_dir = "/root/go-dev/src/imgtask"
app_dir_local = "/Users/apple/go-dev/src/imgtask"
app_linux_bin = "build/linux-amd64/{}".format(app)
app_make_local = "{}/{}".format(app_dir_local, "Makefile")
__app = 'imgtask'
__description = 'Imgfit, APKPure custom imgfit service'
__version = 'v1.1'
__user = __app
__group = __user
__home_path = '/home/' + __user
__install_path = __home_path + '/' + __app
__upload = __home_path + '/upload'
__bin = __install_path + '/imgtask'
__package = "imgtask-linux-amd64-%s.tar.gz"
__package_loc = "file://package/"
fab_package = "/Users/apple/github/fabric_test/base2/package"
# __package_loc = "https://s3.pureapk.com/public/"

#1.压缩文件--local
def pack():
    with lcd(app_dir_local):
        with settings(warn_only=True):
            if not local("test -f %s" % app_make_local).failed:
                local("make clean")
                local("make docker")
                package = __package % __version
                local("cp {}/build/linux-amd64/{} {}".format(app_dir_local, package, fab_package))

#2.上传---local->remote
@roles('realserver1')
def put_task():
    dst = "{}/{}".format(app_dir, app)
    src = "{}/{}".format(app_dir_local, app_linux_bin)
    print(dst, src)
    with cd(app_dir):
        run("pwd")
        run("ls -l")
        put(src, dst)

#3.更新文件
@roles('realserver1')
def clean_app():
    with cd(app_dir):
        run("pwd")
        run('rm -rf {}'.format(app))
        run('ls -l ')

@roles("realserver1")
def imgtask_run():
    # with cd(app_dir):
    #     with settings(warn_only=True):
    #         if not run("test -f {}".format(app)).failed:
    #             run("./{} worker &".format(app))
    systemd_name = __app
    cmd = [__bin, ' worker']
    systemd.setup_simple(systemd_name,
                         app_run_cmd=' '.join(cmd),
                         work_path=__install_path,
                         run_user=__user,
                         run_group=__group,
                         description=__description,
                         addition_service_setting={'LimitNOFILE': 40960},
                         )
    systemd.restart(systemd_name)
    systemd.status(systemd_name)

@roles('realserver1')
def task():
    pack()
    clean_app
    put_task()
    imgtask_run()

def test():
    execute(task)

########################################
# @task
@roles('realserver1')
def imgtask_up_bin(force_update=True):
    """
    更新二进制文件
    """
    version = utils.get_host_setting('imgfit_version', __version)
    if not force_update:
        with quiet():
            print __bin
            v = run(__bin + " version")
            if v.succeeded and version in v:
                return

    utils.ensure_user(__user, True)
    run('mkdir -p ' + __install_path)
    run('mkdir -p ' + __upload)
    run('chown  %s:%s  %s/* -R '%(__user, __group, __home_path))
    package_file = __package % version

    with cd(__install_path):
        pack()
        clean_app()
        put_task()
        utils.put_file(__package_loc+package_file, package_file)
        run('tar xvf %s ' % package_file)
        run('rm -f ' + package_file)
        run('chmod +x ' + __bin)

# @task
@roles('realserver1')
def imgtask_worker_up():
    """
    启动imgtask
    """
    imgtask_up_bin(False)
    # conf = dict(
    #     AccessKey=utils.get_host_setting('imgfit_s3_accesskey', 'YHGIZY2NY26FAE3DXOM8'),
    #     SecretKey=utils.get_host_setting('imgfit_s3_secretkey', 'FPVPcBLQXRFGB4hfQaNFiZaV2AYSSlUdxJLRKSrU'),
    #     Name=utils.get_host_setting('imgfit_s3_region', 'OVH-BHS'),
    #     Host=utils.get_host_setting('imgfit_s3_host', 'http://127.0.0.1:7480'),
    #     Listen=utils.get_host_setting('imgfit_listen','0.0.0.0:8000'),
    #     BucketAllow=utils.get_host_setting('imgfit_BucketAllow', ["image", "upload"]),
    #     )
    # conf_path = __install_path + "/config.json"
    # utils.put_mem_buf(json.dumps(conf), conf_path)
    systemd_name = __app + "_worker"
    cmd = [__bin, ' worker']

    systemd.setup_simple(systemd_name,
                         app_run_cmd=' '.join(cmd),
                         work_path=__install_path,
                         run_user=__user,
                         run_group=__group,
                         description=__description,
                         addition_service_setting={'LimitNOFILE': 40960},
                         )
    systemd.restart(systemd_name)
    systemd.status(systemd_name)

@roles('realserver1')
def imgtask_serve_up():
    """
    启动imgtask
    """
    imgtask_up_bin(False)
    # conf = dict(
    #     AccessKey=utils.get_host_setting('imgfit_s3_accesskey', 'YHGIZY2NY26FAE3DXOM8'),
    #     SecretKey=utils.get_host_setting('imgfit_s3_secretkey', 'FPVPcBLQXRFGB4hfQaNFiZaV2AYSSlUdxJLRKSrU'),
    #     Name=utils.get_host_setting('imgfit_s3_region', 'OVH-BHS'),
    #     Host=utils.get_host_setting('imgfit_s3_host', 'http://127.0.0.1:7480'),
    #     Listen=utils.get_host_setting('imgfit_listen','0.0.0.0:8000'),
    #     BucketAllow=utils.get_host_setting('imgfit_BucketAllow', ["image", "upload"]),
    #     )
    # conf_path = __install_path + "/config.json"
    # utils.put_mem_buf(json.dumps(conf), conf_path)
    systemd_name = __app + "_serve"
    cmd = [__bin, ' serve']

    systemd.setup_simple(systemd_name,
                         app_run_cmd=' '.join(cmd),
                         work_path=__install_path,
                         run_user=__user,
                         run_group=__group,
                         description=__description,
                         addition_service_setting={'LimitNOFILE': 40960},
                         )
    systemd.restart(systemd_name)
    systemd.status(systemd_name)

部署方法

fab -f fab_imgtask_real.py imgtask_worker_up

-f: 指定运行的文件
imgtask_worker_up: 执行程序的入口

运行效果:

base2 git:(master) ✗ fab -f fab_imgtask_real.py imgtask_worker_up
[root@192.168.9.3:22] Executing task 'imgtask_worker_up'
/home/imgtask/imgtask/imgtask
[root@192.168.9.3:22] run: mkdir -p /home/imgtask/imgtask
[root@192.168.9.3:22] run: mkdir -p /home/imgtask/upload
[root@192.168.9.3:22] run: chown  imgtask:imgtask  /home/imgtask/* -R
[localhost] local: test -f /Users/apple/go-dev/src/imgtask/Makefile
[localhost] local: make clean
go clean -i  ./
rm -f imgtask
rm -rf linux
[localhost] local: make docker
docker run --rm -v "`pwd`":/go/src/imgtask -w /go/src/imgtask golang:latest bash -c "make build && make package"
mkdir -p build/`go env GOHOSTOS`-`go env GOHOSTARCH`
go build -ldflags "-X main.AppVersion=`git describe --tags` -X main.BuildTime=`date '+%Y-%m-%d_%H:%M:%S'`"  -o build/`go env GOHOSTOS`-`go env GOHOSTARCH`/imgtask ./
cd build/`go env GOHOSTOS`-`go env GOHOSTARCH`/ &&  tar zcvf imgtask-`go env GOHOSTOS`-`go env GOHOSTARCH`-`git describe --tags`.tar.gz imgtask
imgtask
[localhost] local: cp /Users/apple/go-dev/src/imgtask/build/linux-amd64/imgtask-linux-amd64-v1.1.tar.gz /Users/apple/github/fabric_test/base2/package
[root@192.168.9.3:22] run: pwd
[root@192.168.9.3:22] out: /root/go-dev/src/imgtask
[root@192.168.9.3:22] out:
[root@192.168.9.3:22] run: rm -rf imgtask
[root@192.168.9.3:22] run: ls -l
[root@192.168.9.3:22] out: total 12
[root@192.168.9.3:22] out: drwxr-xr-x. 2 root root 4096 Feb  6 03:45 conf
[root@192.168.9.3:22] out: drwxr-xr-x. 2 root root 4096 Feb  6 03:45 configs
[root@192.168.9.3:22] out: drwxr-xr-x. 2 root root 4096 Feb  6 03:18 tmp
[root@192.168.9.3:22] out:
('/root/go-dev/src/imgtask/imgtask', '/Users/apple/go-dev/src/imgtask/build/linux-amd64/imgtask')
[root@192.168.9.3:22] run: pwd
[root@192.168.9.3:22] out: /root/go-dev/src/imgtask
[root@192.168.9.3:22] out:
[root@192.168.9.3:22] run: ls -l
[root@192.168.9.3:22] out: total 12
[root@192.168.9.3:22] out: drwxr-xr-x. 2 root root 4096 Feb  6 03:45 conf
[root@192.168.9.3:22] out: drwxr-xr-x. 2 root root 4096 Feb  6 03:45 configs
[root@192.168.9.3:22] out: drwxr-xr-x. 2 root root 4096 Feb  6 03:18 tmp
[root@192.168.9.3:22] out:
[root@192.168.9.3:22] put: /Users/apple/go-dev/src/imgtask/build/linux-amd64/imgtask -> /root/go-dev/src/imgtask/imgtask
[root@192.168.9.3:22] put: package/imgtask-linux-amd64-v1.1.tar.gz -> /home/imgtask/imgtask/imgtask-linux-amd64-v1.1.tar.gz
[root@192.168.9.3:22] run: tar xvf imgtask-linux-amd64-v1.1.tar.gz
[root@192.168.9.3:22] out: imgtask
[root@192.168.9.3:22] out:
[root@192.168.9.3:22] run: rm -f imgtask-linux-amd64-v1.1.tar.gz
[root@192.168.9.3:22] run: chmod +x /home/imgtask/imgtask/imgtask
[Sending]: /etc/systemd/system/imgtask_worker.service
[root@192.168.9.3:22] run: chmod 644 /etc/systemd/system/imgtask_worker.service
[root@192.168.9.3:22] run: systemctl daemon-reload
[root@192.168.9.3:22] run: systemctl enable imgtask_worker.service
[root@192.168.9.3:22] run: systemctl restart imgtask_worker.service
[root@192.168.9.3:22] run: systemctl status imgtask_worker.service
[root@192.168.9.3:22] out: ● imgtask_worker.service - Imgfit, APKPure custom imgfit service
[root@192.168.9.3:22] out:    Loaded: loaded (/etc/systemd/system/imgtask_worker.service; enabled; vendor preset: disabled)
[root@192.168.9.3:22] out:    Active: active (running) since Mon 2017-02-06 15:45:49 EST; 92ms ago
[root@192.168.9.3:22] out:  Main PID: 7754 (imgtask)
[root@192.168.9.3:22] out:    CGroup: /system.slice/imgtask_worker.service
[root@192.168.9.3:22] out:            └─7754 /home/imgtask/imgtask/imgtask worker
[root@192.168.9.3:22] out:
[root@192.168.9.3:22] out: Feb 06 15:45:49 c3 imgtask[7754]: 2017/02/06 15:45:49 [I] [configs.go:111] [*] ReportAddr:
[root@192.168.9.3:22] out: Feb 06 15:45:49 c3 imgtask[7754]: 2017/02/06 15:45:49 [I] [configs.go:111] [*] SSDBHost:
[root@192.168.9.3:22] out: Feb 06 15:45:49 c3 imgtask[7754]: 2017/02/06 15:45:49 [I] [configs.go:111] [*] DlServAddr:
[root@192.168.9.3:22] out: Feb 06 15:45:49 c3 imgtask[7754]: 2017/02/06 15:45:49 [I] [configs.go:111] [*] S3Host: http://192.168.0.18:7380
[root@192.168.9.3:22] out: Feb 06 15:45:49 c3 imgtask[7754]: 2017/02/06 15:45:49 [I] [configs.go:111] [*] S3AccessKey: UH0YAIEY9O703LJNG97E
[root@192.168.9.3:22] out: Feb 06 15:45:49 c3 imgtask[7754]: 2017/02/06 15:45:49 [I] [configs.go:111] [*] S3SecretKey: 9oTBmFUGbaXQmAf2OPM8vtN2IzPeXHFfGDRNn4uK
[root@192.168.9.3:22] out: Feb 06 15:45:49 c3 imgtask[7754]: 2017/02/06 15:45:49 [I] [configs.go:111] [*] S3ImageAccessKey:
[root@192.168.9.3:22] out: Feb 06 15:45:49 c3 imgtask[7754]: 2017/02/06 15:45:49 [I] [configs.go:111] [*] S3ImageSecretKey:
[root@192.168.9.3:22] out: Feb 06 15:45:49 c3 imgtask[7754]: 2017/02/06 15:45:49 [I] [worker.go:50] dlserv(dev) worker start.
[root@192.168.9.3:22] out: Feb 06 15:45:49 c3 imgtask[7754]: 2017/02/06 15:45:49 [I] [ippoll.go:17] iplist:[https://p.xgj.me:27035]
[root@192.168.9.3:22] out:
Done.
➜  base2 git:(master) ✗

查看运行状态和log

1.systemctl status imgtask_worker.service 运行效果:

    [root@c3 imgtask]# systemctl status imgtask_worker.service
    ● imgtask_worker.service - Imgfit, APKPure custom imgfit service
       Loaded: loaded (/etc/systemd/system/imgtask_worker.service; enabled; vendor preset: disabled)
       Active: active (running) since 一 2017-02-06 15:45:49 EST; 14min ago
     Main PID: 7754 (imgtask)
       CGroup: /system.slice/imgtask_worker.service
               └─7754 /home/imgtask/imgtask/imgtask worker

    2月 06 15:59:10 c3 imgtask[7754]: 2017/02/06 15:59:10 [D] [worker_common.go:86] Processing dlimg task, id: 77
    2月 06 15:59:20 c3 imgtask[7754]: 2017/02/06 15:59:20 [E] [worker_common_dl_img.go:75] result:{"package":"com.tencent.mobileqqi","context":"","images":{"icon":null,"screenshort":null,"banner":null,"tube":null}}  ...err:403 Forbidden
    2月 06 15:59:20 c3 imgtask[7754]: 2017/02/06 15:59:20 [D] [worker_common.go:88] dlimg task done, id: 77
    2月 06 15:59:20 c3 imgtask[7754]: 2017/02/06 15:59:20 [D] [worker_common.go:86] Processing dlimg task, id: 77
    2月 06 15:59:31 c3 imgtask[7754]: 2017/02/06 15:59:31 [E] [worker_common_dl_img.go:75] result:{"package":"com.tencent.mobileqqi","context":"","images":{"icon":null,"screenshort":null,"banner":null,"tube":null}}  ...err:403 Forbidden
    2月 06 15:59:31 c3 imgtask[7754]: 2017/02/06 15:59:31 [D] [worker_common.go:88] dlimg task done, id: 77
    2月 06 15:59:31 c3 imgtask[7754]: 2017/02/06 15:59:31 [D] [worker_common.go:86] Processing dlimg task, id: 77
    2月 06 15:59:44 c3 imgtask[7754]: 2017/02/06 15:59:44 [E] [worker_common_dl_img.go:75] result:{"package":"com.tencent.mobileqqi","context":"","images":{"icon":null,"screenshort":null,"banner":null,"tube":null}}  ...err:403 Forbidden
    2月 06 15:59:44 c3 imgtask[7754]: 2017/02/06 15:59:44 [D] [worker_common.go:88] dlimg task done, id: 77
    2月 06 15:59:44 c3 imgtask[7754]: 2017/02/06 15:59:44 [D] [worker_common.go:86] Processing dlimg task, id: 77
    Hint: Some lines were ellipsized, use -l to show in full.
    [root@c3 imgtask]#

2.journalctl -u imgtask_worker -f (journalctl 配合 systemd 查看log)

[root@c3 imgtask]# journalctl -u imgtask_worker  -f
-- Logs begin at 一 2017-02-06 01:54:38 EST. --
2月 06 16:01:45 c3 imgtask[7754]: 2017/02/06 16:01:45 [D] [worker_common.go:86] Processing dlimg task, id: 77
2月 06 16:01:49 c3 imgtask[7754]: 2017/02/06 16:01:49 [E] [worker_common_dl_img.go:75] result:{"package":"com.tencent.mobileqqi","context":"","images":{"icon":null,"screenshort":null,"banner":null,"tube":null}}  err:dlImgUrlBase(uploadImg) err:err:uploadImg(w.s3fs.PutFile2)  err:PutFile2(b.PutReader)  err:403 Forbidden  r:0xc4203fa6c0  bucket:image filetype:image/png key:com.tencent.mobileqqi_0_70857c01 size:650772    err1:403 Forbidden  url:https://image.winudf.com/v1/image/YWRtaW5fbmE2dTlteXI/=200.jpg  fid:image_com.tencent.mobileqqi_0_70857c01
2月 06 16:01:49 c3 imgtask[7754]: 2017/02/06 16:01:49 [D] [worker_common.go:88] dlimg task done, id: 77
2月 06 16:01:49 c3 imgtask[7754]: 2017/02/06 16:01:49 [D] [worker_common.go:86] Processing dlimg task, id: 77
2月 06 16:01:53 c3 imgtask[7754]: 2017/02/06 16:01:53 [E] [worker_common_dl_img.go:75] result:{"package":"com.tencent.mobileqqi","context":"","images":{"icon":null,"screenshort":null,"banner":null,"tube":null}}  err:dlImgUrlBase(uploadImg) err:err:uploadImg(w.s3fs.PutFile2)  err:PutFile2(b.PutReader)  err:403 Forbidden  r:0xc4203ca6f0  bucket:image filetype:image/png key:com.tencent.mobileqqi_0_1b51a91a size:650772    err1:403 Forbidden  url:https://image.winudf.com/v1/image/YWRtaW5fbmE2dTlteXI/=200.jpg  fid:image_com.tencent.mobileqqi_0_1b51a91a

bug 处理

© 著作权归作者所有

上一篇: gitlab ci
下一篇: python fabric
hyhlinux
粉丝 8
博文 167
码字总数 51777
作品 0
海淀
程序员
私信 提问
CentOS7 - 快速查看系统日志

  前言   systemd拥有强大的处理与系统日志记录功能-systemd-journald。日志目录一般是在/var/log/journal,记录的是二进制文件,我们可以通过journalctl进行查看。      常用的操作...

linux运维菜
2018/10/23
0
0
RH124-10 系统日志的分析及管理

第十章 系统日志的分析及管理 10.1 系统日志架构分析 在rhel7系统中有两个日志服务,分别是传统的rsyslog和新添加的systemd-journal. systemd-journald是一个改进型的日志管理服务,可以收集...

myworldkwd
2018/06/26
0
0
Systemd 学习笔记

这两天在配单机多redis实例,发现配好之后在ubuntu15.10上无法自启,看来用sysvinit的方式不灵了,顺便研究了一下systemd,发现这东西挺好使的,不但支持服务自启,还支持job,可以替换cront...

jim19770812
2016/02/13
197
0
Systemd 入门教程:命令篇

Systemd 是 Linux 系统工具,用来启动守护进程,已成为大多数发行版的标准配置。 本文介绍它的基本用法,分为上下两篇。今天介绍它的主要命令,下一篇介绍如何用于实战。 一、由来 历史上,L...

snowing1990
2016/03/15
125
0
转自Linux运维笔记 - systemd详解

systemd的特性有: 支持并行化任务 同时采用socket式与D-Bus总线式激活服务; 按需启动守护进程(daemon); 利用 Linux 的 cgroups 监视进程; 支持快照和系统恢复; 维护挂载点和自动挂载点...

erichd
2016/07/13
19
0

没有更多内容

加载失败,请刷新页面

加载更多

数组算法

/*数组的相关的算法操作:1、在数组中找最大值/最小值*/class Test11_FindMax{public static void main(String[] args){int[] array = {4,2,6,8,1};//在数组中找最大...

architect刘源源
45分钟前
2
0
okhttp3 以上版本在安卓9.0无法请求数据的解决方案

应用官方的说明:在 Android 6.0 中,我们取消了对 Apache HTTP 客户端的支持。 从 Android 9 开始,默认情况下该内容库已从 bootclasspath 中移除且不可用于应用。且Android P 限制了明文流量...

chenhongjiang
今天
11
0
简单示例:NodeJs连接mysql数据库

开篇引用网上的说法: 简单的说 Node.js 就是运行在服务端的 JavaScript。Node.js 是一个基于Chrome JavaScript 运行时建立的一个平台。Node.js是一个事件驱动I/O服务端JavaScript环境,基于...

李朝强
今天
8
0
大数据学习路线

年薪30W大数据学习路线图: 一、Hadoop入门,了解什么是Hadoop 1、Hadoop产生背景 2、Hadoop在大数据、云计算中的位置和关系 3、国内外Hadoop应用案例介绍 4、国内Hadoop的就业情况分析及课程...

陈小君
今天
3
0
解读 Kylin 3.0.0 | 更敏捷、更高效的 OLAP 引擎

在近期的 Apache Kylin Meetup 成都站上,我们邀请到 Kyligence 架构师 & Apache Kylin Committer 倪春恩对 Kylin 3.0.0 版本的一些重要功能及改进从使用到原理进行了介绍: Apache Kylin 在...

ApacheKylin
今天
6
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部