Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add database migrations as an Ant target #9823

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 47 additions & 8 deletions java/manager-build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
<property name="branding.src.dir" value="${basedir}/../branding/java/code/src"/>
<property name="frontend.src.dir" value="${basedir}/../web/html/src"/>
<property name="frontend.dist.dir" value="${basedir}/../web/html/src/dist"/>
<property name="schema.src.dir" value="${basedir}/../schema/spacewalk/upgrade"/>

<property name="ssh.socket.file" value="${user.home}/.ssh/manager-build-tunnel-${deploy.host}-${deploy.user}"/>
<property name="ssh.socket.option" value="-o ControlPath=${ssh.socket.file}"/>
Expand Down Expand Up @@ -119,30 +120,44 @@
</condition>

<!-- macros for deployment -->
<macrodef name="deploy-directory">
<macrodef name="deploy-to-temp-directory">
<attribute name="source"/>
<attribute name="destination"/>
<attribute name="syncParameters" default=""/>
<element name="tasks" implicit="true" optional="true" />
<sequential>
<echo message="Creating temp directory" level="verbose" />
<!-- Define a temporary directory or fallback to /tmp/deploy-dir -->
<local name="deploy.temp.dir" />
<deploy-execute outputProperty="deploy.temp.dir" command="mktemp -d" />
<property name="deploy.temp.dir" value="/tmp/deploy-temp-dir" />

<echo message="Deploying directory @{source} to @{destination} using ${deploy.temp.dir}" />

<!-- Extract to the temporary directory -->
<echo message="Executing sh -c &quot;tar c -C @{source} -f - . | ${deploy.executor.command} ${deploy.executor.parameters} 'tar xf - -C ${deploy.temp.dir}/'&quot;" level="verbose"/>
<exec failonerror="true" executable="sh" logerror="true">
<arg line="-c &quot;tar c -C @{source} -f - . | ${deploy.executor.command} ${deploy.executor.parameters} 'tar xf - -C ${deploy.temp.dir}/ --no-same-owner --no-same-permissions'&quot;"/>
</exec>
<!-- Use rsync to synchronize the folder -->
<deploy-execute command="rsync -a @{syncParameters} ${deploy.temp.dir}/ @{destination}" />
<!-- Remove the temporary stuff -->

<tasks/>

<echo message="Cleaning up temp directory ${deploy.temp.dir}" level="verbose" />
<!-- Cleanup -->
<deploy-execute command="rm -rf ${deploy.temp.dir}" />
</sequential>
</macrodef>

<macrodef name="deploy-directory">
<attribute name="source"/>
<attribute name="destination"/>
<attribute name="syncParameters" default=""/>
<sequential>
<deploy-to-temp-directory source="@{source}">
<echo message="Deploying directory @{source} to @{destination} using ${deploy.temp.dir}" />

<!-- Use rsync to synchronize the folder -->
<deploy-execute command="rsync -a @{syncParameters} ${deploy.temp.dir}/ @{destination}" />
</deploy-to-temp-directory>
</sequential>
</macrodef>

<macrodef name="deploy-execute">
<attribute name="command"/>
<attribute name="outputProperty" default="" />
Expand Down Expand Up @@ -643,4 +658,28 @@
<echo message="This target is obsolete and will be removed. Please use:" />
<echo message=" ant -f manager-build.xml -Ddeploy.mode=container deploy-restart" />
</target>

<target name="apply-schema-migrations" depends="check-deploy-mode, ensure-server-access" description="Apply all available schema migrations">
<echo message="Applying all schema migrations..."/>
<sequential>
<deploy-to-temp-directory source="${schema.src.dir}">
<echo message="Stopping spacewalk-service..." />
<deploy-execute command="spacewalk-service stop" />

<property name="match" value="susemanager-schema-5.*" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know it's probably not possible to do better than this in plain ant, but this is not totally correct and might lead to errors.

<property name="argline" value="-c &quot;${deploy.executor.command} ${deploy.executor.parameters} 'find ${deploy.temp.dir}/${match} -type f -name &quot;*.sql&quot; -exec spacewalk-sql --select-mode-direct {} \\\;'&quot;" />

<echo message="Finding and applying migrations with:" level="verbose" />
<echo message="${argline}" level="verbose" />
<local name="result" />
<exec failonerror="true" executable="sh" logerror="true" outputproperty="result">
<arg line="${argline}"/>
</exec>
Comment on lines +675 to +677
Copy link
Contributor

@mackdk mackdk Mar 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem here is that the escaping of the \; is only correct when running in remote-container mode. When using a different mode, since the command is not passed around in the same way, find breaks and cannot identify the -exec parameter, as reported by @parlt91.

On top of that, I'm not sure if find is able to sort the directories in the correct order (i.e. 5.0.9 and 5.0.10)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I selected the wrong lines. I was referring to the argline:

<property name="argline" value="-c &quot;${deploy.executor.command} ${deploy.executor.parameters} 'find ${deploy.temp.dir}/${match} -type f -name &quot;*.sql&quot; -exec spacewalk-sql --select-mode-direct {} \\\;'&quot;" /> 

<echo message="${result}" />

<echo message="Starting spacewalk-service..." />
<deploy-execute command="spacewalk-service start" />
</deploy-to-temp-directory>
</sequential>
</target>
</project>