github.com/klaytn/klaytn@v1.12.1/build/rpm/main.go (about)

     1  package main
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"os"
     7  	"strings"
     8  	"text/template"
     9  
    10  	"github.com/klaytn/klaytn/params"
    11  	"github.com/urfave/cli/v2"
    12  )
    13  
    14  const (
    15  	CN   = "kcn"
    16  	PN   = "kpn"
    17  	EN   = "ken"
    18  	SCN  = "kscn"
    19  	SPN  = "kspn"
    20  	SEN  = "ksen"
    21  	BN   = "kbn"
    22  	HOMI = "homi"
    23  	GEN  = "kgen"
    24  )
    25  
    26  type NodeInfo struct {
    27  	daemon      string
    28  	summary     string
    29  	description string
    30  }
    31  
    32  var BINARY_TYPE = map[string]NodeInfo{
    33  	CN: {
    34  		"kcnd",
    35  		"Klaytn consensus node daemon",
    36  		"kcnd is a daemon for Klaytn consensus node (kcn). For more information, please refer to https://docs.klaytn.com.",
    37  	},
    38  	PN: {
    39  		"kpnd",
    40  		"Klaytn proxy node daemon",
    41  		"kpnd is a daemon for Klaytn proxy node (kpn). For more information, please refer to https://docs.klaytn.com.",
    42  	},
    43  	EN: {
    44  		"kend",
    45  		"Klaytn endpoint node daemon",
    46  		"kend is a daemon for Klaytn endpoint node (ken). For more information, please refer to https://docs.klaytn.com.",
    47  	},
    48  	SCN: {
    49  		"kscnd",
    50  		"Klaytn servicechain consensus node daemon",
    51  		"kscnd is a daemon for Klaytn servicechain consensus node (kscn). For more information, please refer to https://docs.klaytn.com.",
    52  	},
    53  	SPN: {
    54  		"kspnd",
    55  		"Klaytn servicechain proxy node daemon",
    56  		"kspnd is a daemon for Klaytn servicechain proxy node (kspn). For more information, please refer to https://docs.klaytn.com.",
    57  	},
    58  	SEN: {
    59  		"ksend",
    60  		"Klaytn servicechain endpoint node daemon",
    61  		"ksend is a daemon for Klaytn servicechain endpoint node (ksen). For more information, please refer to https://docs.klaytn.com.",
    62  	},
    63  	BN: {
    64  		"kbnd",
    65  		"Klaytn boot node daemon",
    66  		"kbnd is a daemon for Klaytn boot node (kbn). For more information, please refer to https://docs.klaytn.com.",
    67  	},
    68  	HOMI: {
    69  		"homi",
    70  		"genesis.json generator",
    71  		"homi is a generator of genesis.json.",
    72  	},
    73  	GEN: {
    74  		"kgen",
    75  		"private key generator",
    76  		"kgen is a generator of private keys.",
    77  	},
    78  }
    79  
    80  type RpmSpec struct {
    81  	BuildNumber int
    82  	Version     string
    83  	Name        string
    84  	Summary     string
    85  	MakeTarget  string
    86  	ProgramName string // kcn, kpn, ken, kscn, kspn, ksen, kbn
    87  	DaemonName  string // kcnd, kpnd, kend, kscnd, kspnd, ksend, kbnd
    88  	PostFix     string // baobab
    89  	Description string
    90  }
    91  
    92  func (r RpmSpec) String() string {
    93  	tmpl, err := template.New("rpmspec").Parse(rpmSpecTemplate)
    94  	if err != nil {
    95  		fmt.Printf("Failed to parse template, %v", err)
    96  		return ""
    97  	}
    98  
    99  	result := new(bytes.Buffer)
   100  	err = tmpl.Execute(result, r)
   101  	if err != nil {
   102  		fmt.Printf("Failed to render template, %v", err)
   103  		return ""
   104  	}
   105  	return result.String()
   106  }
   107  
   108  func main() {
   109  	app := cli.NewApp()
   110  	app.Name = "klaytn_rpmtool"
   111  	app.Version = "0.2"
   112  	app.Commands = []*cli.Command{
   113  		{
   114  			Name:    "gen_spec",
   115  			Aliases: []string{"a"},
   116  			Usage:   "generate rpm spec file",
   117  			Flags: []cli.Flag{
   118  				&cli.StringFlag{
   119  					Name:  "binary_type",
   120  					Usage: "Klaytn binary type (kcn, kpn, ken, kscn, kspn, ksen, kbn, kgen, homi)",
   121  				},
   122  				&cli.BoolFlag{
   123  					Name:  "devel",
   124  					Usage: "generate spec for devel version",
   125  				},
   126  				&cli.BoolFlag{
   127  					Name:  "baobab",
   128  					Usage: "generate spec for baobab version",
   129  				},
   130  				&cli.IntFlag{
   131  					Name:  "build_num",
   132  					Usage: "build number",
   133  				},
   134  			},
   135  			Action: genspec,
   136  		},
   137  		{
   138  			Name:    "version",
   139  			Aliases: []string{"v"},
   140  			Usage:   "return klaytn version",
   141  			Action: func(c *cli.Context) error {
   142  				fmt.Print(params.Version)
   143  				return nil
   144  			},
   145  		},
   146  		{
   147  			Name:    "release_num",
   148  			Aliases: []string{"r"},
   149  			Usage:   "return klaytn release number",
   150  			Action: func(c *cli.Context) error {
   151  				fmt.Print(params.ReleaseNum)
   152  				return nil
   153  			},
   154  		},
   155  	}
   156  
   157  	if err := app.Run(os.Args); err != nil {
   158  		fmt.Fprintln(os.Stderr, err)
   159  		os.Exit(1)
   160  	}
   161  }
   162  
   163  func genspec(c *cli.Context) error {
   164  	rpmSpec := new(RpmSpec)
   165  
   166  	binaryType := c.String("binary_type")
   167  	if _, ok := BINARY_TYPE[binaryType]; ok != true {
   168  		return fmt.Errorf("binary_type[\"%s\"] is not supported. Use --binary_type [kcn, kpn, ken, kscn, kspn, ksen, kbn, kgen, homi]", binaryType)
   169  	}
   170  
   171  	rpmSpec.ProgramName = strings.ToLower(binaryType)
   172  	rpmSpec.DaemonName = BINARY_TYPE[binaryType].daemon
   173  	rpmSpec.PostFix = ""
   174  
   175  	if c.Bool("devel") {
   176  		buildNum := c.Int("build_num")
   177  		if buildNum == 0 {
   178  			fmt.Println("BuildNumber should be set")
   179  			os.Exit(1)
   180  		}
   181  		rpmSpec.BuildNumber = buildNum
   182  		rpmSpec.Name = BINARY_TYPE[binaryType].daemon + "-devel"
   183  	} else if c.Bool("baobab") {
   184  		rpmSpec.BuildNumber = params.ReleaseNum
   185  		rpmSpec.Name = BINARY_TYPE[binaryType].daemon + "-baobab"
   186  		rpmSpec.PostFix = "_baobab"
   187  	} else {
   188  		rpmSpec.BuildNumber = params.ReleaseNum
   189  		rpmSpec.Name = BINARY_TYPE[binaryType].daemon
   190  	}
   191  	rpmSpec.Summary = BINARY_TYPE[binaryType].summary
   192  	rpmSpec.Description = BINARY_TYPE[binaryType].description
   193  	rpmSpec.Version = params.Version
   194  	fmt.Println(rpmSpec)
   195  	return nil
   196  }
   197  
   198  var rpmSpecTemplate = `Name:               {{ .Name }}
   199  Version:            {{ .Version }}
   200  Release:            {{ .BuildNumber }}%{?dist}
   201  Summary:            {{ .Summary }}
   202  
   203  Group:              Application/blockchain
   204  License:            GNU
   205  URL:                https://www.klaytn.com
   206  Source0:            %{name}-%{version}.tar.gz
   207  BuildRoot:          %(mktemp -ud %{_tmppath}/%{name}-%{version}-%{release}-XXXXXX)
   208  
   209  %description
   210    {{ .Description }}
   211  
   212  %prep
   213  %setup -q
   214  
   215  %build
   216  make {{ .ProgramName }}
   217  
   218  %define is_daemon %( if [ {{ .ProgramName }} != {{ .DaemonName }} ]; then echo "1"; else echo "0"; fi )
   219  
   220  %install
   221  mkdir -p $RPM_BUILD_ROOT/usr/bin
   222  mkdir -p $RPM_BUILD_ROOT/etc/{{ .DaemonName }}/conf
   223  mkdir -p $RPM_BUILD_ROOT/etc/init.d
   224  mkdir -p $RPM_BUILD_ROOT/var/log/{{ .DaemonName }}
   225  
   226  cp build/bin/{{ .ProgramName }} $RPM_BUILD_ROOT/usr/bin/{{ .ProgramName }}
   227  %if %is_daemon
   228  cp build/rpm/etc/init.d/{{ .DaemonName }} $RPM_BUILD_ROOT/etc/init.d/{{ .DaemonName }}
   229  cp build/rpm/etc/{{ .DaemonName }}/conf/{{ .DaemonName }}{{ .PostFix }}.conf $RPM_BUILD_ROOT/etc/{{ .DaemonName }}/conf/{{ .DaemonName }}.conf
   230  %endif
   231  
   232  %files
   233  %attr(755, -, -) /usr/bin/{{ .ProgramName }}
   234  %if %is_daemon
   235  %config(noreplace) %attr(644, -, -) /etc/{{ .DaemonName }}/conf/{{ .DaemonName }}.conf
   236  %attr(754, -, -) /etc/init.d/{{ .DaemonName }}
   237  %endif
   238  %exclude /usr/local/var/lib/rpm/*
   239  %exclude /usr/local/var/lib/rpm/.*
   240  %exclude /usr/local/var/tmp/*
   241  
   242  %pre
   243  %if %is_daemon
   244  if [ $1 -eq 2 ]; then
   245  	# Package upgrade
   246  	systemctl stop {{ .DaemonName }}.service > /dev/null 2>&1
   247  fi
   248  %endif
   249  
   250  %post
   251  %if %is_daemon
   252  if [ $1 -eq 1 ]; then
   253  	# Package installation
   254  	systemctl daemon-reload >/dev/null 2>&1
   255  fi
   256  if [ $1 -eq 2 ]; then
   257  	# Package upgrade
   258  	systemctl daemon-reload >/dev/null 2>&1
   259  fi
   260  %endif
   261  
   262  %preun
   263  %if %is_daemon
   264  if [ $1 -eq 0 ]; then
   265  	# Package removal, not upgrade
   266  	systemctl --no-reload disable {{ .DaemonName }}.service > /dev/null 2>&1
   267  	systemctl stop {{ .DaemonName }}.service > /dev/null 2>&1
   268  fi
   269  %endif
   270  
   271  %postun
   272  %if %is_daemon
   273  if [ $1 -eq 0 ]; then
   274  	# Package uninstallation
   275  	systemctl daemon-reload >/dev/null 2>&1
   276  fi
   277  %endif
   278  `