Shell implements deployment of ftp to provide shared yum warehouse

shell

    • Script implementation
    • script test

The script realizes automatic configuration of vsftp and shares the opt directory with other node machines to realize offline source configuration.

To use the script, you need to modify the following:
ftp_ip is the ftp server ip
reponame is the repo file name
dir is the warehouse directory, which is under opt by default.
houzui is an offline warehouse in the opt directory, and is written to houzui as an array.
Before use, you need to ensure that the server can download the vsftp service.

Script implementation

#!/bin/bash

ftp_ip="192.168.200.80"
reponame="local.repo"
houzui=("centos" "iaas")
opt="/opt"

if [ "$(hostname -I | awk '{print $1}')" = "$ftp_ip" ]; then
    systemctl stop firewalld
    setenforce 0
    yum install -y vsftpd
    echo "anon_root=$opt" >> /etc/vsftpd/vsftpd.conf
    systemctl enable --now vsftpd
    systemctl restart vsftpd
    rm -rf /etc/yum.repos.d/*
    for repo in "${houzui[@]}"; do
        cat >> "/etc/yum.repos.d/$reponame" <<eof
[$repo]
name=$repo
baseurl=file://$opt/$repo
gpgcheck=0
enabled=1
eof
    done
    yum clean all
    yum repolist
else
    rm -rf /etc/yum.repos.d/*
    for repo in "${houzui[@]}"; do
        cat >> "/etc/yum.repos.d/$reponame" <<eof
[$repo]
name=$repo
baseurl=ftp://$ftp_ip/$repo
gpgcheck=0
enabled=1
eof
    done
    yum clean all
    yum repolist
fi

Script test

Node IP
Node 1 192.168.200.80
Node 2 192.168.200.81
Node 3 192.168.200.82

Manually configure the offline warehouse

[root@localhost ~]# mkdir /opt/{centos,iaas}
[root@localhost ~]# mount /dev/sr0 /opt/centos/
mount: /dev/sr0 is write-protected, mounting read-only
[root@localhost ~]# cp -rf /opt/centos/* /opt/iaas/

Script deployment

[root@localhost ~]# cat repo.sh
#!/bin/bash

ftp_ip="192.168.200.80"
reponame="local.repo"
houzui=("centos" "iaas")
opt="/opt"

if [ "$(hostname -I | awk '{print $1}')" = "$ftp_ip" ]; then
    systemctl stop firewalld
    setenforce 0
    yum install -y vsftpd
    echo "anon_root=$opt" >> /etc/vsftpd/vsftpd.conf
    systemctl enable --now vsftpd
    systemctl restart vsftpd
    rm -rf /etc/yum.repos.d/*
    for repo in "${houzui[@]}"; do
        cat >> "/etc/yum.repos.d/$reponame" <<eof
[$repo]
name=$repo
baseurl=file://$opt/$repo
gpgcheck=0
enabled=1
eof
    done
    yum clean all
    yum repolist
else
    rm -rf /etc/yum.repos.d/*
    for repo in "${houzui[@]}"; do
        cat >> "/etc/yum.repos.d/$reponame" <<eof
[$repo]
name=$repo
baseurl=ftp://$ftp_ip/$repo
gpgcheck=0
enabled=1
eof
    done
    yum clean all
    yum repolist
fi
[root@localhost ~]# hostname -I
192.168.200.80
[root@localhost ~]# bash repo.sh
[root@localhost ~]# cat /etc/yum.repos.d/local.repo
[centos]
name=centos
baseurl=file:///opt/centos
gpgcheck=0
enabled=1
[iaas]
name=iaas
baseurl=file:///opt/iaas
gpgcheck=0
enabled=1
[root@localhost ~]# yum clean all;yum repolist
Loaded plugins: fastestmirror
Cleaning repos: centos iaas
Cleaning up list of fastest mirrors
Other repos take up 195 M of disk space (use --verbose for details)
Loaded plugins: fastestmirror
Determining fastest mirrors
centos | 3.6 kB 00:00:00
iaas | 3.6 kB 00:00:00
(1/4): centos/group_gz | 153 kB 00:00:00
(2/4): centos/primary_db | 3.3 MB 00:00:00
(3/4): iaas/group_gz | 153 kB 00:00:00
(4/4): iaas/primary_db | 3.3 MB 00:00:00
repo id repo name status
centos centos 4,070
iaas iaas 4,070
repolist: 8,140
[root@localhost ~]#

Verify from node

[root@localhost ~]# cat /etc/yum.repos.d/local.repo
[centos]
name=centos
baseurl=ftp://192.168.200.80/centos
gpgcheck=0
enabled=1
[iaas]
name=iaas
baseurl=ftp://192.168.200.80/iaas
gpgcheck=0
enabled=1
[root@localhost ~]# yum clnen all;yum repolist
Loaded plugins: fastestmirror
No such command: clnen. Please use /usr/bin/yum --help
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
repo id repo name status
centos centos 4,070
iaas iaas 4,070
repolist: 8,140
[root@localhost ~]# hostname -I
192.168.200.81
[root@localhost ~]#
[root@localhost ~]# bash repo.sh
Loaded plugins: fastestmirror
Cleaning repos: centos iaas
Cleaning up list of fastest mirrors
Loaded plugins: fastestmirror
Determining fastest mirrors
centos | 3.6 kB 00:00:00
iaas | 3.6 kB 00:00:00
(1/4): iaas/group_gz | 153 kB 00:00:00
(2/4): centos/group_gz | 153 kB 00:00:00
(3/4): centos/primary_db | 3.3 MB 00:00:00
(4/4): iaas/primary_db | 3.3 MB 00:00:00
repo id repo name status
centos centos 4,070
iaas iaas 4,070
repolist: 8,140
[root@localhost ~]# hostname -I
192.168.200.82
[root@localhost ~]# cat /etc/yum.repos.d/local.repo
[centos]
name=centos
baseurl=ftp://192.168.200.80/centos
gpgcheck=0
enabled=1
[iaas]
name=iaas
baseurl=ftp://192.168.200.80/iaas