# coding: utf-8
import os
import time
import json
import subprocess
import pandas as pd
import streamlit as st
st.set_page_config(
page_title="Airflow Deployment Platform",
page_icon="🚀",
layout="wide",
initial_sidebar_state="expanded",
)
st.markdown("""
<style>
.big-font {
font-size:22px
}
</style>
""", unsafe_allow_html=True)
@st.cache(show_spinner=False, suppress_st_warning=True, ttl=120)
def load_airflow_dags():
dags_json = os.popen("airflow dags list -o json")
data = dags_json.readlines()[1]
return data
with st.sidebar:
st.title("📊 部署和更新Airflow任务")
option = st.selectbox("任务方式", ('更新', '部署'))
airflow_home = os.environ.get("AIRFLOW_HOME")
if option == "更新":
st.title("更新Airflow任务")
st.markdown("[🚀访问Airflow ](http://airflow.com:9000/)")
with st.spinner("🔎 后台查询任务中,请稍等..."):
dags_json = os.popen("airflow dags list -o json")
data = load_airflow_dags()
df = pd.DataFrame.from_dict(json.loads(data))
df = df.loc[df["owner"].isin(["link", "yumingmin", "mock"])].reset_index(drop=True)
dag_id, owner, update_col = st.columns(3)
dag_id.markdown("**DAG_ID**")
owner.markdown("**OWNER**")
update_col.markdown("**UPDATE**")
for raw in df.values:
project_path = os.path.join(airflow_home, "dags", raw[1].split("/")[0])
dag_id.markdown('<p class="big-font">%s</p>' % raw[0], unsafe_allow_html=True)
owner.markdown('<p class="big-font">%s</p>' % raw[2], unsafe_allow_html=True)
if update_col.button("更新", key=raw[0]):
update_cmd = subprocess.Popen(f"cd {project_path} && git pull", shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = update_cmd.communicate()
update_cmd.wait()
succ_flag = 1 if 'up to date' in str(stdout) or 'done' in str(stdout) else 0
if succ_flag == 1:
with st.spinner("😁更新成功!!"):
time.sleep(5)
else:
with st.spinner("😂更新失败!!"):
time.sleep(5)
if option == "部署":
st.title("部署Airflow任务")
st.markdown("[🚀访问Airflow ](http://airflow.com:9000/)")
form = st.form(key="Deploy-Project")
gitlab_url = form.text_input('输入 GitLab 项目地址: 例如 git@github.com:yumingmin/testisok.git')
submit = form.form_submit_button("部署")
if submit:
if gitlab_url:
with st.spinner("⏳ 后台部署中,请稍等..."):
deploy_cmd = subprocess.Popen(f"cd {airflow_home}/dags && git clone {gitlab_url}",
shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = deploy_cmd.communicate()
deploy_cmd.wait()
succ_flag = 1 if 'Cloning' in str(stderr) else 0
st.text("out %s" % str(stderr))
st.text("err %s" % str(stderr))
if succ_flag == 1:
with st.spinner("😁部署成功!!"):
time.sleep(5)
else:
with st.spinner("😂部署失败!!"):
time.sleep(5)
else:
with st.spinner("🚧 必须填写项目地址!!!"):
time.sleep(5)
终端执行:
$ nohup streamlit run update_gitlab_projects_dashboard.py --server.port 9001 > streamlit_run.log &