App Engine:更新/运行时启动脚本

发布于 2024-10-06 13:05:41 字数 486 浏览 3 评论 0原文

我正在使用 App Engine,并且正在考虑在下一个项目中使用 LESS CSS 扩展。没有用 Python 编写的好的 LESS CSS 库,所以我继续使用原始的 Ruby 库,它运行良好且开箱即用。我希望 App Engine 在运行开发服务器之前以及将文件上传到云之前执行 lessc ./templates/css/style.less。自动化此操作的最佳方法是什么?我在想:

#run.sh:
lessc ./templates/css/style.less
.gae/dev_appserver.py --use_sqlite .

我是否

#deploy.sh
lessc ./templates/css/style.less
.gae/appcfg.py update .

走在正确的道路上,或者是否有更优雅的做事方式,也许是在 appcfg.py 级别?

谢谢。

I'm working with App Engine and I'm thinking about using the LESS CSS extension in my next project. There's no good LESS CSS library written in Python so I went on with the original Ruby one which works great and out of the box. I'd like App Engine to execute lessc ./templates/css/style.less before running the development server and before uploading the files to the cloud. What is the best way to automate this? I'm thinking:

#run.sh:
lessc ./templates/css/style.less
.gae/dev_appserver.py --use_sqlite .

And

#deploy.sh
lessc ./templates/css/style.less
.gae/appcfg.py update .

Am I on the correct path or is there a more elegant way of doing things, perhaps at the appcfg.py level?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无人问我粥可暖 2024-10-13 13:05:41

一种选择是使用 Less 的 javascript 版本,从而在浏览器中进行 less-to-css 转换。只需上传 less 格式的文件(请参阅 http://lesscss.org/ 了解详细信息)。

或者,我在部署脚本中进行转换(首先使用 less,现在我使用 sass),该脚本执行许多操作,

  • 检查我的源代码控件是否没有签出未完成的文件(未提交的更改),
  • 加入并缩小我的 .js 代码(并在其上运行 jslint)到单个文件中
  • 生成其他内容(包括将源代码控制版本作为版本号标记到某些关键文件中以及作为某些文件上的参数以避免缓存问题),因此我的主页使用 URL 提取脚本例如“allmysource.js?v=585”..该文件可能是静态的,但添加的参数会强制缓存失效,
  • 调用 appcfg 来执行上传并检查返回代码
  • 对真实站点进行一些调用使用 wget 检查之前生成的文件是否实际返回,通过检查它们是否标记有预期版本
  • 应用另一个源代码控制标记来表明预期版本已成功部署

我的脚本还接受“-preview”标志,在这种情况下它实际上并不执行上传,而是报告自上次部署以来发生的更改的版本控制注释。

me@here $ ./deploy -preview
Deployment preview...
Would deploy v596 to the production site (currently v593, previously v587)
  594       Fix blah blah blah for X Y Z
  595       New feature nah nah nah 
  596       Update help pages

这非常方便,可以提醒我需要在更改日志之类的内容中放入什么内容,

我还计划扩展它,以便我可以作为源代码控制的一部分,添加仅在部署时需要运行一次的任何代码(例如数据库架构更改)并知道当我下次部署新版本时它将自动运行。

正如人们所问的,下面脚本的本质...它没有显示我的“检查代码、生成、加入和缩小”,因为这是另一个脚本...我意识到最初的问题当然是询问该步骤:)但你可以看到在哪里添加生成 CSS 等的调用

#!/bin/sh

function abort () {
    echo
    echo "ERROR: $1"
    echo "$2"
    exit 99
}

function warn () {
    echo
    echo "WARNING: $1"
    echo "$2"
}

# Overrides the Gentoo eselect mechanism to force the python version the GAE scripts expect
export EPYTHON=python2.5

# names of tags used to label bzr versions    
CURR_DTAG=deployed
PREV_DTAG=prevDeployed

# command line options
PREVIEW=0
IGNORE_BZR=0

# These next few vars are set to values to identify my site, insert your own values here...
APPID=your_gae_appid_here
ADMIN_EMAIL=your_admin_email_address_here
SRCDIR=directory_to_deploy
CHECK_URL=url_of_page_to_retrive_that_does_upload_initialisation

for ARG; do
    if [[ "$ARG" == "-preview" ]]; then
        echo "Deployment preview..."
        PREVIEW=1
    fi
    if [[ "$ARG" == "-force" ]]; then
        echo "Ignoring the fact some files may not be committed to bzr..."
        IGNORE_BZR=1
    fi
done
echo

# check bzr for uncommited changed    
BSTATUS=`bzr status`
if [[ "$BSTATUS" != "" ]]; then
    if [[ "$IGNORE_BZR" == "0" ]]; then 
        abort "There are uncommited changes - commit/revert/ignore all files before deploying" "$BSTATUS"
    else
        warn  "There are uncommited changes" "$BSTATUS"
    fi
fi

# get version of numbers of last deployed etc
currver=`bzr log -l1 --line                | sed -e 's/: .*//'`
lastver=`bzr log -rtag:${CURR_DTAG} --line | sed -e 's/: .*//'`
prevver=`bzr log -rtag:${PREV_DTAG} --line | sed -e 's/: .*//'`
lastlog=`bzr log -l 1 --line gae/changelog | sed -e 's/: .*//'`

RELEASE_NOTES=`bzr log --short --forward -r $lastver..$currver \
    | perl -ne '$ver = $1 if /^ {0,4}(\d+) /; print "  $ver $_" if ($ver and /^ {5,}\w/)' \
    | grep -v "^ *$lastver "`

LOG_NOTES=`bzr log --short --forward -r $lastlog..$currver \
    | perl -ne '$ver = $1 if /^ {0,4}(\d+) /; print "  $ver $_" if ($ver and /^ {5,}\w/)' \
    | grep -v "^ *$lastlog "`

# Crude but old habit - BUGBUGBUG is a marker in the code for things to be fixed before deployment
echo "Checking code for outstanding issues before deployment"
BUGSTATUS=`grep BUGBUGBUG js/*js`
if [[ "$BUGSTATUS" != "" ]]; then
    if [[ "$IGNORE_BZR" == "0" ]]; then 
        abort "There are outstanding BUGBUGBUGs - fix them before deploying" "$BUGSTATUS"
    else
        warn  "There are outstanding BUGBUGBUGs" "$BUGSTATUS"
    fi
fi

echo
echo "Deploy v$currver to the production site (currently v$lastver, previously v$prevver)"
echo "$RELEASE_NOTES"
echo

if [[ "$currver" -gt "$lastlog" && "$lastver" -ne "$lastlog" ]]; then
    echo "Changes since the changelog was last updated"
    echo "$LOG_NOTES"
    echo
fi

if [[ "$IGNORE_BZR" == "0" && $lastver -ge $currver ]]; then
    abort "There don't appear to be any changes to deploy..."
fi

if [[ "$PREVIEW" == "1" ]]; then
    exit 0
fi

$EPYTHON -c "import ssl" \
    || abort "$EPYTHON can't find ssl module for $EPYTHON - download it from pypi and install with the inbuilt setup.py"

# REMOVED - call to my script that calls jslint, generates files and compresses JS etc
#   || abort "Generation of code failed"

/opt/google_appengine/appcfg.py --email=$ADMIN_EMAIL -v -A $APPID update $SRCDIR \
    || abort "Appcfg failed - upload presumably incomplete"

# move the tags to show we deployed properly
bzr tag -r $lastver --force ${PREV_DTAG}
bzr tag -r $currver --force ${CURR_DTAG}

echo
echo "Production site updated from v$lastver to v$currver (in turn from v$prevver)"
echo
echo "Now visiting $CHECK_URL to upload the source to the database"

# new version doesn't seem to always be there (may be caching by the webserver etc) to be uploaded into the database.. try again just in case
for cb in $RANDOM $RANDOM $RANDOM $RANDOM ; do
    prodver=`wget $CHECK_URL?_cb=$cb -q -O - | perl -ne 'print $1 if /^\s*Rev #(\d+)\s*$/'`
    if [[ "$currver" == "$prodver" ]]; then
        echo "OK: New version $prodver successfully deployed"
        exit 0
    fi
    echo "Retrying the upload of source to the database"
    sleep 5
done
abort "The new source doesn't seem to be loading into the database" "Try 'wget $CHECK_URL?_cb=$RANDOM -q -O -'"

它不是特别大或聪明,但它可以自动执行上传作业

One option is to use the javascript version of Less and hence do the less-to-css conversion in the browser.. simply upload your less formatted file (see http://lesscss.org/ for details).

Alternately, I do the conversion (first with less, now I use sass) in a deploy script which does a number of things

  • checks that my source code control has no outstanding files checked out (uncommited changes)
  • joins and minifies my .js code (and runs jslint over it) into a single file
  • generates other content (including stamping the source code control version as a version number into certain key files and as a parameter on some files to avoid caching issues) so my main page pulls in scripts with URLs such as "allmysource.js?v=585".. the file might be static but the added params force cache invalidation
  • calls appcfg to perform the upload and checks the return code
  • makes some calls to the real site with wget to check the previously generated files are actually returned, by checking they're stamped with the expected version
  • applies another source code control tag to say that the intended version was successfully deployed

My script also accepts a "-preview" flag in which case it doesn't actually do the upload, but reports the version control comments for what's changed since the previous deployment.

me@here $ ./deploy -preview
Deployment preview...
Would deploy v596 to the production site (currently v593, previously v587)
  594       Fix blah blah blah for X Y Z
  595       New feature nah nah nah 
  596       Update help pages

This is pretty handy as a reminder of what I need to put in things like a changelog

I plan to also expand it so that I can, as part of my source code control, add any code that needs running once only when deployed (eg database schema changes) and know that it'll be automatically run when I next deploy a new version.

Essence of the script below as people asked... it doesn't show my "check code, generate, join, and minify" as that's another script... I realise that the original question was asking about that step of course :) but you can see where you'd add the call to generate CSS etc

#!/bin/sh

function abort () {
    echo
    echo "ERROR: $1"
    echo "$2"
    exit 99
}

function warn () {
    echo
    echo "WARNING: $1"
    echo "$2"
}

# Overrides the Gentoo eselect mechanism to force the python version the GAE scripts expect
export EPYTHON=python2.5

# names of tags used to label bzr versions    
CURR_DTAG=deployed
PREV_DTAG=prevDeployed

# command line options
PREVIEW=0
IGNORE_BZR=0

# These next few vars are set to values to identify my site, insert your own values here...
APPID=your_gae_appid_here
ADMIN_EMAIL=your_admin_email_address_here
SRCDIR=directory_to_deploy
CHECK_URL=url_of_page_to_retrive_that_does_upload_initialisation

for ARG; do
    if [[ "$ARG" == "-preview" ]]; then
        echo "Deployment preview..."
        PREVIEW=1
    fi
    if [[ "$ARG" == "-force" ]]; then
        echo "Ignoring the fact some files may not be committed to bzr..."
        IGNORE_BZR=1
    fi
done
echo

# check bzr for uncommited changed    
BSTATUS=`bzr status`
if [[ "$BSTATUS" != "" ]]; then
    if [[ "$IGNORE_BZR" == "0" ]]; then 
        abort "There are uncommited changes - commit/revert/ignore all files before deploying" "$BSTATUS"
    else
        warn  "There are uncommited changes" "$BSTATUS"
    fi
fi

# get version of numbers of last deployed etc
currver=`bzr log -l1 --line                | sed -e 's/: .*//'`
lastver=`bzr log -rtag:${CURR_DTAG} --line | sed -e 's/: .*//'`
prevver=`bzr log -rtag:${PREV_DTAG} --line | sed -e 's/: .*//'`
lastlog=`bzr log -l 1 --line gae/changelog | sed -e 's/: .*//'`

RELEASE_NOTES=`bzr log --short --forward -r $lastver..$currver \
    | perl -ne '$ver = $1 if /^ {0,4}(\d+) /; print "  $ver $_" if ($ver and /^ {5,}\w/)' \
    | grep -v "^ *$lastver "`

LOG_NOTES=`bzr log --short --forward -r $lastlog..$currver \
    | perl -ne '$ver = $1 if /^ {0,4}(\d+) /; print "  $ver $_" if ($ver and /^ {5,}\w/)' \
    | grep -v "^ *$lastlog "`

# Crude but old habit - BUGBUGBUG is a marker in the code for things to be fixed before deployment
echo "Checking code for outstanding issues before deployment"
BUGSTATUS=`grep BUGBUGBUG js/*js`
if [[ "$BUGSTATUS" != "" ]]; then
    if [[ "$IGNORE_BZR" == "0" ]]; then 
        abort "There are outstanding BUGBUGBUGs - fix them before deploying" "$BUGSTATUS"
    else
        warn  "There are outstanding BUGBUGBUGs" "$BUGSTATUS"
    fi
fi

echo
echo "Deploy v$currver to the production site (currently v$lastver, previously v$prevver)"
echo "$RELEASE_NOTES"
echo

if [[ "$currver" -gt "$lastlog" && "$lastver" -ne "$lastlog" ]]; then
    echo "Changes since the changelog was last updated"
    echo "$LOG_NOTES"
    echo
fi

if [[ "$IGNORE_BZR" == "0" && $lastver -ge $currver ]]; then
    abort "There don't appear to be any changes to deploy..."
fi

if [[ "$PREVIEW" == "1" ]]; then
    exit 0
fi

$EPYTHON -c "import ssl" \
    || abort "$EPYTHON can't find ssl module for $EPYTHON - download it from pypi and install with the inbuilt setup.py"

# REMOVED - call to my script that calls jslint, generates files and compresses JS etc
#   || abort "Generation of code failed"

/opt/google_appengine/appcfg.py --email=$ADMIN_EMAIL -v -A $APPID update $SRCDIR \
    || abort "Appcfg failed - upload presumably incomplete"

# move the tags to show we deployed properly
bzr tag -r $lastver --force ${PREV_DTAG}
bzr tag -r $currver --force ${CURR_DTAG}

echo
echo "Production site updated from v$lastver to v$currver (in turn from v$prevver)"
echo
echo "Now visiting $CHECK_URL to upload the source to the database"

# new version doesn't seem to always be there (may be caching by the webserver etc) to be uploaded into the database.. try again just in case
for cb in $RANDOM $RANDOM $RANDOM $RANDOM ; do
    prodver=`wget $CHECK_URL?_cb=$cb -q -O - | perl -ne 'print $1 if /^\s*Rev #(\d+)\s*$/'`
    if [[ "$currver" == "$prodver" ]]; then
        echo "OK: New version $prodver successfully deployed"
        exit 0
    fi
    echo "Retrying the upload of source to the database"
    sleep 5
done
abort "The new source doesn't seem to be loading into the database" "Try 'wget $CHECK_URL?_cb=$RANDOM -q -O -'"

It's not particularly big or clever, but it automates the upload job

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文