Koozali.org formerly Contribs.org

Contribs.org Forums => Development => Topic started by: DanB35 on February 24, 2016, 01:19:29 PM

Title: [Solved] Template fragment question
Post by: DanB35 on February 24, 2016, 01:19:29 PM
I'm having trouble getting a template fragment to work.  It's throwing errors which I expect relate to my not having escaped everything I need to escape.  Here's what I need to appear in the output:

Code: [Select]
if [ $1 = "deploy_cert" ] && [ $2 = "pbx.mydomain.tld" ]; then
  KEY=$3
  CERT=$4
  CHAIN=${5/fullchain.pem/chain.pem}
  scp $CERT root@pbx:/etc/pki/tls/certs/pbx.mydomain.tld.crt
  scp $KEY root@pbx:/etc/pki/tls/private/pbx.mydomain.tld.key
  scp $CHAIN root@pbx:/etc/pki/tls/certs/server-chain.crt
  ssh root@pbx "/sbin/service httpd reload"
  exit 0
fi

Here's what I have in the fragment:
Code: [Select]
{
    use strict;
    use warnings;
    use esmith::ConfigDB;

    my $configDB = esmith::ConfigDB->open_ro or die("can't open Config DB");

    my $letsencryptStatus = $configDB->get_prop( 'letsencrypt', 'status' )     || 'disabled';

    if ( $letsencryptStatus ne 'disabled' ) {

        $OUT .= "if [ \$1 = \"deploy_cert\" ] && [ \$2 = \"pbx.mydomain.tld\" ]; then\n"
        $OUT .= "  KEY=\$3\n"
        $OUT .= "  CERT=\$4\n"
        $OUT .= "  CHAIN=\${5/fullchain.pem/chain.pem}\n"
        $OUT .= "  scp \$CERT root@pbx:/etc/pki/tls/certs/pbx.mydomain.tld.crt\n"
        $OUT .= "  scp \$KEY root@pbx:/etc/pki/tls/private/pbx.mydomain.tld.key\n"
        $OUT .= "  scp \$CHAIN root@pbx:/etc/pki/tls/certs/server-chain.crt\n"
        $OUT .= "  ssh root@pbx \"/sbin/service httpd reload\"\n"
        $OUT .= "  exit 0\n"
        $OUT .= "fi\n"
    }
}

...but when I try to expand the template, I get these errors:
Code: [Select]
[root@e-smith hook-script.sh]# expand-template /usr/local/bin/hook-script.sh
WARNING in /etc/e-smith/templates-custom//usr/local/bin/hook-script.sh/05deploy_cert_PBX: Scalar found where operator expected at /etc/e-smith/templates-custom//usr/local/bin/hook-script.sh/05deploy_cert_PBX line 13, near ""if [ \$1 = \"deploy_cert\" ] && [ \$2 = \"pbx.familybrown.org\" ]; then\n"
        $OUT"
WARNING in /etc/e-smith/templates-custom//usr/local/bin/hook-script.sh/05deploy_cert_PBX: (Missing operator before
        $OUT?)
ERROR in /etc/e-smith/templates-custom//usr/local/bin/hook-script.sh/05deploy_cert_PBX: Program fragment delivered error <<syntax error at /etc/e-smith/templates-custom//usr/local/bin/hook-script.sh/05deploy_cert_PBX line 13, near ""if [ \$1 = \"deploy_cert\" ] && [ \$2 = \"pbx.familybrown.org\" ]; then\n"
        $OUT ">> at template line 1
ERROR: Template processing failed for //usr/local/bin/hook-script.sh: 2 fragments generated warnings, 1 fragment generated errors
 at /sbin/e-smith/expand-template line 45

I'm assuming that I missed escaping something, but it isn't obvious to me at first glance what it is.  Any suggestions?
Title: Re: Template fragment question
Post by: Daniel B. on February 24, 2016, 01:36:39 PM
You forgot the semi-colon at the end of your $OUT = XXX lines

But anyway, you'd better have something like

Code: [Select]
    $OUT .=<<"_EOF";
if [ \$1 = "deploy_cert" ] && [ \$2 = "pbx.mydomain.tld" ]; then
  KEY=\$3
  CERT=\$4
  CHAIN=\${5/fullchain.pem/chain.pem}
  scp $CERT root\@pbx:/etc/pki/tls/certs/pbx.mydomain.tld.crt
  scp $KEY root\@pbx:/etc/pki/tls/private/pbx.mydomain.tld.key
  scp $CHAIN root\@pbx:/etc/pki/tls/certs/server-chain.crt
  ssh root\@pbx "/sbin/service httpd reload"
  exit 0
fi
_EOF

(In this case, as your not using any perl variable, you could even use <<'_EOF' in which case there's no need to escape $ and @)
Title: Re: Template fragment question
Post by: DanB35 on February 24, 2016, 02:33:21 PM
You forgot the semi-colon at the end of your $OUT = XXX lines
Well, I figured I'd feel dumb when I learned the answer, and I do--but now I know.  Thanks!

Quote
    $OUT .=<<"_EOF";
So much the better.  For the sake of posterity, I note that I also had to escape the $ in $CERT, etc.  It seems to be working now.  Thanks!
Title: Re: Template fragment question
Post by: guest22 on March 02, 2016, 02:03:01 PM
Please add [Solved] in front of the subject.
Title: Re: Template fragment question
Post by: DanB35 on March 02, 2016, 02:24:17 PM
It won't let me edit the first post to change the subject.
Title: Re: Template fragment question
Post by: guest22 on March 02, 2016, 02:27:37 PM
Bummer, thanks for trying.
Title: Re: Template fragment question
Post by: janet on March 02, 2016, 08:35:56 PM
It won't let me edit the first post to change the subject.

I think editing is allowed only for 7 days from the original post date.
Title: Re: Template fragment question
Post by: Withave on August 17, 2016, 10:40:25 AM
Well, I figured I'd feel dumb when I learned the answer, and I do--but now I know.  Thanks!
So much the better.  For the sake of posterity, I note that I also had to escape the $ in $CERT, etc.  It seems to be working now.  Thanks!

I also tried it and yeah it works! you just forgot the semi-colon at the end of your $OUT = XXX lines.. It's a good thing to know then!
Title: Re: [Solved] Template fragment question
Post by: ReetP on August 18, 2016, 09:19:11 PM
Dan,

If you have a mod to the contrib then let me know and I can update it. I'm not on here so often so miss stuff. Just mail me.

B. Rgds
John