搭建企业私有yum仓库并打自己第一个rpm包

私有yum仓库的好处

  • 减少rpm包下载网络开销
  • 加速下载rpm包
  • 便于管理自定义rpm包
  • 具有企业特色

如何搭建私有yum仓库

创建自己yum仓库路径
1
mkdir /www/yum_store/centos/7/meitu/x86_64/
  • 现在仓库是空的,我们简单下载几个软件放到我们的私有仓库中:
1
yum install  --downloadonly --downloaddir=/www/yum_store/centos/7/meitu/x86_64/ nfs-utils --downloadonly  (只下载不安装) --downloaddir   (指定rpm包的下载路径)
安装createrepo工具
1
yum -y install createrepo
生成repodata索引
1
createrepo -pdo /www/yum_store/centos/7/meitu/x86_64/ /www/yum_store/centos/7/meitu/x86_64/
为你的仓库提供web访问能力

这个可以用很多方式,用nginx、apache或者其他,我这边做演示,简单启动一个python的web服务即可。

1
2
cd /www/yum_store/centos/
python -m SimpleHTTPServer 80

你可以在浏览器中访问你的web服务,可以得到如下的信息:

1
http://localhost:80/7/meitu/x86_64/

rpmlist

配置yum源,repo文件(暂无配置签名)
1
2
3
4
5
6
7
vim /etc/yum.repos.d/my-meitu.repo

[meitu]
name=my-meitu-repo
baseurl=http://localhost:80/$releasever/meitu/$basearch
enable=1
gpgcheck=0
刷新yum源,查看私有仓库配置是否成功
1
2
3
4
5
[root@node1 ~]# yum clean all
[root@node1 ~]# yum repolist
repo id repo name status
meitu/7/x86_64 my-meitu-repo 16
repolist: 16
查看自己yum仓库中的rpm包有哪些

myreporpmlist

安装其中一个软件测试

查看输出信息,你会发现该软件以及该软件的依赖都来自于你的私有仓库,另外你也可以看看你的web服务日志,会打印出一些日志。

1
yum install nfs-utils.x86_64

installrpmlog

如何打自己第一个rpm包

目前有提供两种打包方式,第一rpmbuild,第二种使用FPM
相对来说rpmbuild会比较复杂,我是更喜欢fpm一点,所以今天简单讲解一下fpm,如果对rpmbuild有兴趣的同学,我后续也可以单独再写一篇讲解。

整体流程

  • 把你的软件安装在一个临时目录里
  • 修改一些自定义的配置
  • 写两个脚本:安装后执行脚本,卸载后执行脚本
  • 打包,生成rpm包

安装打包工具

  • fpm是ruby写的,所以要装一下ruby环境
    1
    2
    yum -y groupinstall "Development Tools"
    yum -y install ruby ruby-devel rubygems gcc openssl-devel
  • 配置ruby源加速
    1
    2
    3
    4
    5
    6
    gem sources --add http://gems.ruby-china.com/
    gem sources --remove https://rubygems.org/
    [root@node1 ~]# gem sources
    *** CURRENT SOURCES ***

    http://gems.ruby-china.com/
  • 安装fpm
    1
    gem install arr-pm fpm

    制作helloworld软件

    我这边就不已繁琐的软件为例子了,我自己随便写了一个helloworld脚本:
    1
    2
    3
    vim helloworld
    #!/bin/bash
    echo "hello world,this is my first rpm"

    整体的一个目录结构

    安装后脚本:创建一个目录
    卸载后脚本:把helloworld文件删除
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    [root@node1 helloworld]# tree
    .
    ├── install_dir 打包的相对路径
    │   └── usr
    │   └── bin
    │   └── helloworld
    ├── rpm 构建产物rpm存放的位置
    └── scripts
    ├── after_install.sh 安装后脚本
    └── after_remove.sh 卸载后脚本

    5 directories, 3 files
    [root@node1 helloworld]# cat scripts/after_install.sh
    #!/bin/bash
    source /etc/rc.d/init.d/functions
    mkdir /www
    exit $?
    [root@node1 helloworld]# cat scripts/after_remove.sh
    #!/bin/bash
    source /etc/rc.d/init.d/functions
    rm /usr/bin/helloworld -rf
    exit $?
    [root@node1 helloworld]#

    开始构建

    至于fpm的参数,直接看man文档就好了,非常简单,下面就是常用的几个。
    1
    2
    3
    [root@node1 helloworld]# fpm -f -s dir -t rpm -n meituhello -v 1.1.0 --iteration 1.el7 -C ./install_dir/ -p ./rpm/ --description 'meituhello-1.1.0.rpm' --url 'https://www.meitu.com' --after-install scripts/after_install.sh --after-remove scripts/after_remove.sh -m libin --vendor "dgsfor@gmail.com"

    Created package {:path=>"./rpm/meituhello-1.1.0-1.el7.x86_64.rpm"}

    查看rpm包信息

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    [root@node1 rpm]# rpm -qpi meituhello-1.1.0-1.el7.x86_64.rpm
    Name : meituhello
    Version : 1.1.0
    Release : 1.el7
    Architecture: x86_64
    Install Date: (not installed)
    Group : default
    Size : 52
    License : unknown
    Signature : (none)
    Source RPM : meituhello-1.1.0-1.el7.src.rpm
    Build Date : Fri 24 May 2019 02:20:19 PM EDT
    Build Host : node1
    Relocations : /
    Packager : libin
    Vendor : dgsfor@gmail.com
    URL : https://www.meitu.com
    Summary : meituhello-1.1.0.rpm
    Description :
    meituhello-1.1.0.rpm

    安装测试

    安装完之后直接执行meituhello命令,会输出你自定义的信息
    1
    2
    3
    4
    5
    6
    7
    8
    9
    [root@node1 rpm]# rpm -ivh meituhello-1.1.0-1.el7.x86_64.rpm
    Preparing... ################################# [100%]
    Updating / installing...
    1:meituhello-1.1.0-1.el7 ################################# [100%]

    [root@node1 rpm]# ls /usr/bin/helloworld
    /usr/bin/helloworld
    [root@node1 rpm]# helloworld
    hello world,this is my first rpm

    卸载

    理论上卸载会删除helloworld文件
    1
    2
    3
    [root@node1 ~]# rpm -e meituhello-1.1.0-1.el7.x86_64
    [root@node1 ~]# ls /usr/bin/helloworld
    ls: cannot access /usr/bin/helloworld: No such file or directory

把这个包加到我们的私有yum仓库

每新增一个rpm包,都要update一下仓库

1
2
3
4
5
6
7
8
9
[root@node1 ~]# cp rpm/meituhello-1.1.0-1.el7.x86_64.rpm /www/yum_store/centos/7/meitu/x86_64/
[root@node1 ~]# createrepo --update /www/yum_store/centos/7/meitu/x86_64/
Spawning worker 0 with 1 pkgs
Workers Finished
Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Sqlite DBs complete

newrpmlist