需要一点帮助来为 nodejs 应用程序创建 rpm 包

nol*_*mit 3 linux rpm rpmbuild node.js centos6

我已经按照说明准备了一个构建框,我四处搜索并进行了 hello world rpm 构建,但仍然不确定打包一堆 js 文件(nodejs 应用程序)的最佳方法是什么。我想 :

  1. 将文件部署到某个位置
  2. 运行 # npm 安装
  3. 运行命令以启动节点侦听器/守护程序。

我已经完成了一个非常简单的规范文件,如下所示:

Name:           nodejsapp
Version:        1.0
Release:        0
Summary:        API for a Platform

Group:          Group
License:        License
URL:            somewebsite
Distribution:   Linux CentOS
Source0:        %{name}-%{version}.tar.gz

%description
CDR Platform is awesome product.


%prep

%install
install -m 755 -d $RPM_BUILD_ROOT/opt/cdr/api

%clean
rm -rf $RPM_BUILD_ROOT


%files
%defattr(-,root,root,-)
Run Code Online (Sandbox Code Playgroud)

以上构建,但当我安装时没有任何反应!我不确定我是否完全理解 rpmbuild 是如何开展业务的。

那么在这种情况下我需要使用哪个宏?我需要在 %files 下创建一个文件列表吗?在运行 rpmbuild 之前我需要 gzip 文件吗?或者我可以在 rpmbuild 期间 gzip 它们吗?

任何帮助表示赞赏。

Bit*_*Dog 7

可以在此处找到为 Fedora EPEL 存储库打包 nodejs 应用程序的指南。在打包您自己的 nodejs 应用程序时,它们是一个很好的参考。

您没有提到您使用的是哪个版本或哪个发行版,但我假设它是最近的发行版,因此您要启动的服务将在 systemd 下运行。Fedora systemd 打包指南可以在这里找到。

我还假设您使用 github 来管理代码,它是一个纯 JS 应用程序。

对于没有依赖项或仅依赖于已经打包在 EPEL 中的 nodejs 模块的 nodejs 模块/应用程序,以下 SPEC 文件(保存为 SPECS/nodejs-myapp.spec)将起作用。

%define debug_package %{nil}
#
# automatically generate requires and provides from package.json
#
%{?nodejs_find_provides_and_requires}

#
# filter out any false provides created due to dependencies with native components
#
%{?nodejs_default_filter}

#
# name of zip file containing source without .zip extension
#
%define modname myapp

Summary: A nodejs app with a systemd daemon
Name:    nodejs-%{modname}
Group:   Applications/Tools
Version: 0.1
Release: 1
License: Unlicense
URL:     https://github.com/myuser/myapp
Source0: %{modname}-%{version}.zip
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)
BuildArch: noarch
ExclusiveArch: %{nodejs_arches} noarch
BuildRequires: nodejs-packaging
BuildRequires: systemd

%description
A nodejs app that installs as a systemd service

%prep
%setup -q -n %{modname}-%{version}

%build

%{__rm} -f .gitignore
#
# Fedora guidlines do not allow bundled modules
# use nodejs_symlink_deps in the install section to generate
# links based on package.json contents
#
%{__rm} -rf node_modules

%install
rm -rf $RPM_BUILD_ROOT

#
# copy in the module source
#
%{__install} -d  $RPM_BUILD_ROOT%{nodejs_sitelib}
%{__cp} -r $RPM_BUILD_DIR/%{modname}-%{version} $RPM_BUILD_ROOT%{nodejs_sitelib}/%{modname}
#
# link the daemon binaries into sbindir
#
%{__install} -d  $RPM_BUILD_ROOT%{_sbindir}
%{__ln_s} %{nodejs_sitelib}/%{modname}/bin/myappd $RPM_BUILD_ROOT%{_sbindir}/myappd

#
# link in any dependent nodejs modules referenced in package.json
#
%nodejs_symlink_deps

#
# documents will be handled by %doc, so remove them from buildroot
#
%{__rm} -rf $RPM_BUILD_ROOT%{nodejs_sitelib}/%{modname}{CHANGELOG.md,LICENSE.md,README.md,docs}

#
# Create a systemd unit file
#
cat << __EOF > $RPM_BUILD_ROOT%{_unitdir}/myappd.service
[Unit]
Description=MyApp provides the best API
Documentation=man:myapp.service(8)

[Service]
Type=simple
ExecStart=/usr/sbin/myappd

[Install]
WantedBy=multi-user.target
__EOF

%clean
rm -rf $RPM_BUILD_ROOT

%files
%defattr(-,root,root)
%doc CHANGELOG.md LICENSE.md README.md docs
%{nodejs_sitelib}/%{modname}
%{_sbindir}/myappd
%{_unitdir}/myappd.service

%changelog
* Sun Mar 22 2015 BitByteDog
- Initial spec file for myapp
Run Code Online (Sandbox Code Playgroud)

此规范文件还将处理具有已打包为 EPEL 一部分的依赖项的 nodejs 模块。RPM 的自动需要/提供功能使用模块中的 package.json 文件在最终 RPM 包中创建必要的需要依赖项。

应使用以下命令将源 zip 文件从 github 下载到 SOURCES 目录中:

wget -O SOURCES/myapp-0.1.zip https://github.com/myuser/myapp/archive/v0.1.zip
Run Code Online (Sandbox Code Playgroud)

RPM 使用以下命令构建:

rpmbuild -ba SPECS/nodejs-myapp.spec
Run Code Online (Sandbox Code Playgroud)

创建的 systemd 单元文件非常简单,阅读提供的链接以及相关的 systemd 文档以根据您的要求进行定制。您需要使用以下命令启用和启动服务:

systemctl enable myappd.service
systemctl start myappd.service
Run Code Online (Sandbox Code Playgroud)

之后它会自动启动。

如果你想在你的 JS 应用程序中包含捆绑模块,可以使用 npm 来完成。首先添加标签:

BuildRequires: npm
AutoReq: no
AutoProv: no
Run Code Online (Sandbox Code Playgroud)

抑制自动需要/提供生成。然后去掉线

%{__rm} -rf node_modules
Run Code Online (Sandbox Code Playgroud)

从 %build 部分。也换线

%nodejs_symlink_deps
Run Code Online (Sandbox Code Playgroud)

在 %install 中

#
# npm will bundle in all dependent modules from the npm registry
# (bundling is OK for private packages but not for EPEL packages)
#
npm -g -q --production --prefix="${RPM_BUILD_ROOT}%{_prefix}" install
Run Code Online (Sandbox Code Playgroud)

应用程序中捆绑的所有模块和 package.json 中列出的所有依赖项都将捆绑到您的应用程序中,使其成为一个独立的应用程序。

上面命令中npm用法说明

  • npm install在 package/application 目录中执行,并在该目录中包含的包上运行。
  • -g将当前工作目录中的包安装为全局包
  • -q只打印警告和错误
  • --production不要复制 devDependencies
  • --prefix=安装在前缀目录中

效果是将本地目录下的nodejs app复制到${RPM_BUILD_ROOT}%{_prefix}中,准备打包。