通过jenkins触发shell脚本执行rancher2.0自动化容器部署实战1

实现思路:

1)jenkins触发shell命令

2)shell命令调用Python脚本(通过selenium完成自动化容器UI可视化发布)。

3)在linux服务器中,通过容器python-selenium完成python脚本具体命令的执行。

4)通过执行rancher或K8S客户端命令通过yml文件发版参考:

https://blog.csdn.net/chen978616649/article/details/87066933

具体实现方式:

通过jenkins触发shell脚本,此处略过。

  1. shell脚本内容如下:

    #!/bin/sh

    echo "========$1===$2===$3=====$4====$5=========$6==";

    docker run --rm --name dep-docker -v $PWD/myapp:/usr/src/myapp  -w /usr/src/myapp 172.16.101.43:5000/base-python-selenium:1.0.0  python DeployRancher.py $1 $2 $3 $4 $5 $6

  2. DeployRancher.py脚本内容:

#!/usr/bin/python
import sys
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
import datetime
import os

#chrome_driver = os.path.abspath(r"/opt/webDriver/chromedriver")
#os.environ["webdriver.chrome.driver"] = chrome_driver
chrome_capabilities ={
    "browserName": "chrome",
    "version": "",
    "platform": "ANY",
    "javascriptEnabled": True
    #,"webdriver.chrome.driver":chrome_driver
}

print("time_stamp1====" + datetime.datetime.now().strftime('%Y.%m.%d-%H:%M:%S')+"======"+sys.argv[1]+"-----")

#driver = webdriver.Chrome()
driver = webdriver.Remote("http://172.16.101.49:4444/wd/hub", desired_capabilities=chrome_capabilities)

print("time_stamp2====" + datetime.datetime.now().strftime('%Y.%m.%d-%H:%M:%S'))
driver.get("https://10.50.180.101/login")
driver.maximize_window()

print("time_stamp3====" + datetime.datetime.now().strftime('%Y.%m.%d-%H:%M:%S'))
driver.get_screenshot_as_file(r"tmpng/cg-"+datetime.datetime.now().strftime('%Y.%m.%d-%H:%M:%S')+"chrome.png")
driver.find_element_by_id("login-username-local").send_keys("admin") #输入账号
driver.find_element_by_id("login-password-local").send_keys("88888888aB") #输入密码
driver.find_element_by_xpath("//button[text()='Log In']").click()
WebDriverWait(driver, 10).until(lambda x: x.find_element_by_xpath('//input[@placeholder="Search"]').is_displayed())
driver.get("https://10.50.180.101/p/c-9xrvb:project-jb255/workloads")

WebDriverWait(driver, 10).until(lambda x: x.find_element_by_xpath('//input[@placeholder="Search"]').is_displayed())

driver.get_screenshot_as_file(r"tmpng/cg-"+datetime.datetime.now().strftime('%Y.%m.%d-%H:%M:%S')+"chrome.png")
driver.find_element_by_xpath('//input[@placeholder="Search"]').send_keys(sys.argv[1])
try:
    driver.find_element_by_xpath("//td[text()='No containers match the current search.']")
except:
    driver.find_element_by_css_selector("table > thead > tr.fixed-header > th.row-check > input").click()
    driver.find_element_by_xpath('//a[@tabindex="2"]').click()
    driver.find_element_by_xpath("//button[text()='Delete']").click()
    WebDriverWait(driver, 100).until(lambda x: x.find_element_by_xpath("//td[text()='No containers match the current search.']").is_displayed())
driver.find_element_by_xpath("//a[text()='Deploy']").click()
WebDriverWait(driver, 10).until(lambda x: x.find_element_by_xpath('//input[@placeholder="e.g. myapp"]').is_displayed())
driver.find_element_by_xpath('//input[@placeholder="e.g. myapp"]').send_keys(sys.argv[1])
driver.find_element_by_xpath('//input[@placeholder="e.g. ubuntu:xenial"]').clear()
driver.find_element_by_xpath('//input[@placeholder="e.g. ubuntu:xenial"]').send_keys(sys.argv[2])
driver.find_element_by_xpath("//span[text()='Add Port']").click()
driver.find_element_by_xpath('//input[@placeholder="e.g. 8080"]').send_keys(sys.argv[3])
Select(driver.find_element_by_xpath('//td[@data-title="As a"]/select')).select_by_value(sys.argv[4])
driver.find_element_by_xpath('//input[@class="form-control input-sm ember-text-field ember-view"]').send_keys(sys.argv[6])

driver.find_element_by_xpath("//span[text()='Environment Variables']").click()
driver.find_element_by_xpath("//span[text()='Add From Source']").click()

first_select=driver.find_elements_by_class_name('table')[1].find_elements_by_tag_name('select')[0]
first_select.click()
[ i.click() for i  in first_select.find_elements_by_tag_name('option') if i.text.strip()=='Config Map']

second_select=driver.find_elements_by_class_name('table')[1].find_elements_by_tag_name('select')[1]
second_select.click()
[ i.click() for i  in second_select.find_elements_by_tag_name('option')if i.text.strip()==sys.argv[1] ]

driver.get_screenshot_as_file(r"tmpng/cg-"+datetime.datetime.now().strftime('%Y.%m.%d-%H:%M:%S')+"chrome.png")
driver.find_element_by_xpath("//span[text()='Node Scheduling']").click()
driver.find_element_by_xpath("//b[text()='all']").click()
driver.find_element_by_xpath('//input[@class="form-control input-search search form-control ember-text-field ember-view"]').click()
driver.find_element_by_xpath("//div[text()='"+sys.argv[5]+"']").click()
driver.find_element_by_xpath("//button[text()='Show advanced options']").click()
driver.find_element_by_xpath("//span[text()='Security & Host Config']").click()
driver.find_element_by_xpath("//label[text()='Always']").click()

driver.get_screenshot_as_file(r"tmpng/cg-"+datetime.datetime.now().strftime('%Y.%m.%d-%H:%M:%S')+"chrome.png")
driver.find_element_by_xpath("//button[text()='Launch']").click()
driver.quit()
 

你可能感兴趣的:(自动化)